我有以下类型的文档,其中每个人可能有几个名称和相关的功能描述:
新人
name: ana
name: anna
name: ann
feature: A 65-year old woman that has no known health issues but has a medical history of Schizophrenia.
新人
name: tom
name: thomas
name: thimoty
name: tommy
feature: A 32-year old male that is known to be deaf.
新人
.....
我想要的是在python词典中读取此文件,其中每个新人都被识别。
ID为1的人将拥有['ann','anna','ana']的名字
并将具有['一位65岁的女性,她没有已知的健康问题,但有精神分裂症的病史。' ]
有什么建议吗?
答案 0 :(得分:2)
假设您的输入文件为lo.txt
。它可以通过这种方式添加到字典中:
file = open('lo.txt')
final_data = []
feature = []
names = []
for line in file.readlines():
if ("feature") in line:
data = line.replace("\n","").split(":")
feature=data[1]
final_data.append({
'names': names,
'feature': feature
})
names = []
feature = []
if ("name") in line:
data = line.replace("\n","").split(":")
names.append(data[1])
print final_data
答案 1 :(得分:1)
这样的事情可能会起作用
result = {}
f = open("document.txt")
contents = f.read()
info = contents.split('==== new person ===')
for i in range(len(info)):
info[i].split('\n')
names = []
features = []
for j in range(len(info[i])):
info[i][j].split(':')
if info[i][j][0] == 'name':
names.append(info[i][j][1])
else:
features.append(info[i][j][1])
result[i] = {'names': names,'features': features}
print(result)
这应该给你类似的东西:
{0: {'names': ['ana', 'anna', 'ann'], features:['...', '...']}}
e.t.c
答案 2 :(得分:0)
以下是可能适合您的代码:
f = open("documents.txt").readlines()
f = [i.strip('\n') for i in f]
final_condition = f[len(f)-1]
f.remove(final_condition)
names = [i.split(":")[1] for i in f]
the_dict = {}
the_dict["names"] = names
the_dict["features"] = final_condition
print the_dict
它只是将名称拆分为“:”并取结果列表的最后一个元素(名称)并保留列表名称。