我想上面的格式输入格式转换成类型的字典列表。
基本上我想要的是将文件的内容转换成一个字典列表。然而,每次运行代码时,我都会得到相同的输出:[{'Similar:similar5,'Score':score5,'Compound':smi}]。这意味着只有一个字典创建,当我的目标是创建5个字符(每行一个)。 有人可以帮我解决这个问题吗?
dt = [] # Creates a list to store dicts
with open('sample_text.out') as f: # Opens the target text file
for line in f:
if line.startswith('Compound'):
smi = line.split()[1]
dt.append({'Compound' : smi}) # Add smi as a value in a dict inside the list 'dt'
else: # This part will iterate over the next few lines, split them and add them to the growing list of dicts
new_line = line.split()
similar = new_line[0]
score = new_line[1]
print new_line
for dicts in dt:
dicts['Similar'] = similar
dicts['Score'] = score
print dt
在什么时候被你认为此代码创建第二个字典? (什么是dt中的字典:dicts ['teste'] = new_line'打算做什么?) – user2357112
这段代码创建一个字典的唯一部分是'{'Compound':smi}',它只针对行从“Compound”开始。 – user2357112
我真正想要的是创建5个字符(每行一个)。但是我没有做到。 –