我有一个文件:
Fruits:
I love apples
I also love bananas
who even likes pears?
<<<END
Bananas are yellow
apples are not yellow..
<<<END
Vegetables:
Anything green is gross
I don’t like vegetables
<<<END
Peas are disgusting
Who even likes peas
Is potato a vegetable?
<<<END
我想把结尾的行放在&#34;:&#34;例如&#34; Fruits&#34;和&#34;蔬菜&#34;并使它们成为字典的关键。并创建密钥下的每一行,即元组列表。
到目前为止,我有:
def read_file(file):
dic = {}
lst = []
with open(file,'r') as file:
for line in file:
if line.strip("\n") == "<<<END":
continue
elif line.endswith(":\n"):
a = line.strip(":\n")
dic[a] = []
else:
key = line.strip(":\n")
dic[a].append(key)
return dic
我希望程序返回:
{'Fruits': [("I love apples", "I also love bananas", "who even like pears"),("Bananas are yellow", "apples are not yellow..")], 'Vegetables':[("Anything green is gross", "I don't like vegetables"),("Peas are disgusting", "Who even likes peas", "Is potato a vegetable?")]}
它返回:
{'Fruits': ["I love apples", "I also love bananas", "who even like pears","Bananas are yellow", "apples are not yellow.."], Vegetables: ["Anything green is gross", "I don't like vegetables","Peas are disgusting", "Who even likes peas", "Is potato a vegetable?"]}
答案 0 :(得分:1)
以下是一个解决方案,保持您的代码几乎完好无损。这个想法是制作一个列表,直到你得到<<<END
并将元组(从列表中创建)附加到结果列表。
def read_file(file):
dic = {}
lst = []
with open(file,'r') as file:
temp= []
key = None
for line in file:
line = line.strip()
if line == "<<<END":
dic[key].append(tuple([key] + temp))
temp = []
continue
elif line.endswith(":"):
key = line.strip(":")
dic[key] = []
else:
temp.append(line)
return dic
print(read_file('test2.txt'))
输出:
{'Vegetables': [('Vegetables', 'Anything green is gross', 'I don\xe2\x80\x99t like vegetables'), ('Vegetables', 'Peas are disgusting', 'Who even likes peas', 'Is potato a vegetable?')], 'Fruits': [('Fruits', 'I love apples', 'I also love bananas', 'who even likes pears?'), ('Fruits', 'Bananas are yellow', 'apples are not yellow..')]}
答案 1 :(得分:0)
您可以尝试:
def read_file(file):
dic = {}
lst = []
same_dict = False
with open(file,'r') as file:
for line in file:
if line.endswith(":\n"):
a = line.strip(":\n")
dic[a] = []
else:
if line.strip("\n") == "<<<END":
dic[a].append(tuple(lst))
lst = []
else:
key = line.strip("\n")
lst.append(key)
return dic
参考您的新要求,
def read_file(file):
dic = {}
lst = []
same_dict = False
with open(file,'r') as file:
for line in file:
if line.endswith(":\n"):
a = line.strip(":\n")
dic[a] = []
else:
if line.strip("\n") == "<<<END":
dic[a].append(tuple([a]+lst))
lst = []
else:
key = line.strip("\n")
lst.append(key)
return dic
print(read_file('temp.txt'))
希望有所帮助
答案 2 :(得分:0)
我认为你已经完全正确,只是缺少一些步骤来保持不同的数据组分开。试试:
def read_file(path):
d = {}
with open(path, 'r') as f:
lines = f.read().splitlines()
group = None
parts = []
for line in lines:
if line.endswith(":"):
group = line[:-1]
d[group] = []
else:
if line == "<<<END":
# tuple-ize existing data
d[group].append(tuple(parts))
parts = []
else:
# add to existing data
k = line[:-1]
parts.append(line)
return d
对我而言:
>>> read_file('/tmp/1')
{'Vegetables': [('Anything green is gross', 'I don\xe2\x80\x99t like vegetables'), ('Peas are disgusting', 'Who even likes peas', 'Is potato a vegetable?')], 'Fruits': [('I love apples', 'I also love bananas', 'who even likes pears?'), ('Bananas are yellow ', 'apples are not yellow..')]}
>>>
我刚刚通过元组()添加了一个显式的数据转换,并添加了一个临时变量'parts'来跟踪最终字典之外的数据。