Python创建列表文件并编写查询
很抱歉,如果我的问题看起来很简单,但我是python的新手,我找不到解决方法 我有一个“dish.py”文件,其中包括一些子列表Python创建列表文件并编写查询,python,list,search,text-extraction,Python,List,Search,Text Extraction,很抱歉,如果我的问题看起来很简单,但我是python的新手,我找不到解决方法 我有一个“dish.py”文件,其中包括一些子列表 Fruits={"Ap":Apple "Br":Black Mulberry "Ch":Black Cherry } Meals={"BN":Bean "MT":Meat "VG":Vegetable } Legumes={"LN":Green Lentil
Fruits={"Ap":Apple
"Br":Black Mulberry
"Ch":Black Cherry
}
Meals={"BN":Bean
"MT":Meat
"VG":Vegetable
}
Legumes={"LN":Green Lentil
"P": Pea
"PN":Runner Peanut
}
我想在代码中推进dish.py文件,最后,我想在文件内部创建一个查询
with open("/home/user/Py_tut/Cond/dish.py", 'r') as dish:
content = dish.read()
print dish.closed
dm=dict([dish])
nl=[x for x in dm if x[0]=='P']
for x in dm:
x=str(raw_input("Enter word:"))
if x in dm:
print dm[x]
elif x[0]==("P"):
nl.append(x)
print .join( nl)
可能看起来很乱,但是
dm=dict([dish])
我想创建一个用于查询的字典nl=[x代表dm中的x,如果x[0]='P']
我想写以“P”字母开头的单词dish.py
文件有问题。我如何重新组织它
2。问:如何对文件应用查询并提取以“P”开头的单词
非常感谢您dict()
无法加载字符串:
>>> dict("{'a': 1, 'b': 2}")
Traceback (most recent call last):
File "<pyshell#0>", line 1, in <module>
dict("{'a': 1, 'b': 2}")
ValueError: dictionary update sequence element 0 has length 1; 2 is required
\uuuu init\uuuuu.py
import re #Importing RegExp module
def wordsBeginP():
with open("words.txt") as wordsfile: # Querying a file
words = wordsfile.open
parsed = re.sub(r"\n", " ", words) # Replace \n to " "
return [for i in parsed.split(" ") if i[0] == "P"] # Return list of words
dict()
无法加载字符串:
>>> dict("{'a': 1, 'b': 2}")
Traceback (most recent call last):
File "<pyshell#0>", line 1, in <module>
dict("{'a': 1, 'b': 2}")
ValueError: dictionary update sequence element 0 has length 1; 2 is required
\uuuu init\uuuuu.py
import re #Importing RegExp module
def wordsBeginP():
with open("words.txt") as wordsfile: # Querying a file
words = wordsfile.open
parsed = re.sub(r"\n", " ", words) # Replace \n to " "
return [for i in parsed.split(" ") if i[0] == "P"] # Return list of words
因此,我认为你有不止这两个问题。 首先,如果您想包括“硬编码”列表、
dict等,您可能希望包括dish,dish.py位于您的工作目录中
也就是说,如果python文件中的数据结构实际上位于:
最后,您可以在创建的include
(dish)名称空间下搜索文件中命名和包含的所有数据结构
你可能也很感兴趣(尽管有一些警告) 因此,我认为你的问题不止这两个。
首先,如果您想包括“硬编码”列表、dict等,您可能希望包括dish,dish.py位于您的工作目录中
也就是说,如果python文件中的数据结构实际上位于:
最后,您可以在创建的include
(dish)名称空间下搜索文件中命名和包含的所有数据结构
你可能也很感兴趣(尽管有一些警告) 您为什么要在内存中读取dish.py
文件,而不让Python解释器为您解析它?(好吧,除了定义的dict
是用糟糕的语法编写的,但是当它不应该是Python文件时,为什么要称它为dish.py
)我只是不知道如何编写,这就是为什么我编写了一个非常糟糕的形状。我从零开始理解如何解析等等。谢谢你的评论。你为什么要在内存中读取dish.py
文件而不是让Python解释器为你解析它?(好吧,除了定义的dict
是用糟糕的语法编写的,但是当它不应该是Python文件时,为什么要称它为dish.py
)我只是不知道如何编写,这就是为什么我编写了一个非常糟糕的形状。我从零开始理解如何解析等等。无论如何,谢谢你的评论。非常感谢你在我混乱的列表中付出的努力。我是一个新用户,甚至我都不知道列表的正确形式。能给我提供正确的表格真是太好了。顺便说一句,我还尝试实现搜索代码的第二部分。非常感谢您为我的混乱列表付出了努力。我是一个新用户,甚至我都不知道列表的正确形式。能给我提供正确的表格真是太好了。顺便说一下,我还尝试实现搜索代码的第二部分。非常感谢您更正我的列表。我尝试导入json,可能是因为缺乏知识,我有一些问题,但这些问题给了我一个在编写脚本时应该去哪里的点。您的查询解决方案也很好,它启发了我。谢谢你的宝贵贡献。非常感谢你修改我的名单。我尝试导入json,可能是因为缺乏知识,我有一些问题,但这些问题给了我一个在编写脚本时应该去哪里的点。您的查询解决方案也很好,它启发了我。谢谢你的宝贵贡献。
Fruits={"Ap":'Apple',
"Br":'Black Mulberry',
"Ch":'Black Cherry'
}
Meals={"BN":'Bean',
"MT":'Meat',
"VG":'Vegetable'
}
Legumes={"LN":'Green Lentil',
"P":'Pea',
"PN":'Runner Peanut'
}
for f in [dish.Fruits,dish.Meals,dish.Legumes]:
for k,v in f.items():
if k.startswith('P'):
print k,v