我不得不从文本文件制作两个字典的化合物词典。它们在C00001到C20990(C代表复合词)的文本文件中按数字顺序列出,但是,当我打印字典时,所有内容都会混乱出来,而不是按数字顺序排列。我该如何改变它?另外,有没有办法在键的左侧放一个双引号,而在值串的右侧放置另一个双引号?另外,当我尝试打印它以便每个键都有一个新行时,它会告诉我我有一个关键错误(专门用于输入C20990。我假设这是因为它是字典中的最后一个,但这只会发生,如果它已经在数字顺序,它不是,所以这让我担心,在for循环结束时,它会与多个条目发生)为什么我的字典不是按数字顺序排列的,我可以更改分隔符吗?
import sys
entries = []
names = []
a = {} #achiral
c = {} #chiral
for line in file:
if line.startswith("ENTRY"):
s = line[5:len(line)]
s = s.split()
entries.append(s[0])
if line.startswith("NAME"):
s = line[4:len(line)]
s = s.strip()
st = "".join(s+" ")
line=next(file)
check = False
while not line.startswith("FORMULA") and not line[0].isalpha():
s = line.strip()
if s.find("(R)")>=0:
check = True
if s.find("(S)")>=0:
check = True
if s.find("L-")>=0:
check = True
if s.find("D-")>=0:
check = True
st+=s+" "
line = next(file)
st = st.strip()
if not check:
a[entries[-1]] = st
else:
c[entries[-1]] = st
names = []
st = ""
for key in c:
print("{0}: {1}".format(key, c[entries[-1]]))
你可以使用在http://www.grantjenks.com/docs/sortedcontainers/sorteddict.html – Barmar