假设我有以下词典列表:
citation = [{'ID':'101',
'SENTENCE':'This is a theory sample from a book.'
'AUTHOR':'ALEX B.',
'AUTHOR1':'JOHN K.',
'TITLE':'BASIC PROGRAMMING',
'URL':'an.example.com',
'YEAR':'2010'},
{'ID':'102',
'SENTENCE':'This is a theory from book 1 and book 2',
'AUTHOR':'MARINA E.',
'TITLE':'BE A GOOD PROGRAMMER',
'YEAR':'2011',
'AUTHOR1':'STEVE M.',
'AUTHOR2':'DIANE L.',
'TITLE1':'I AM AN ENGINEER',
'YEAR1':'2013',
'VOLUME':'10'},
{.. other data...},
]
我需要将此字典列表保存到csv
文件中。如果字典中的键相似(AUTHOR = AUTHOR1 = AUTHOR2, TITLE = TITLE1 = TITLE2
等),则将其放在相同的列中,列名称(AUTHOR, TITLE, YEAR
)中不带数字。如果列中的数据值不止一个,则应使用分号(;
)分隔。另外,每个字典中的键名及其顺序有时与列表中的其他字典不同。
这是我的代码,但是由于字典中相似的键存储为不同的字段名而无法正常工作
outpath = 'mycitation.csv'
outfile = open(outpath, 'w')
fields = (list(set(k for d in citation for k in d)))
writer = csv.DictWriter(outfile, fieldnames=field, dialect='excel')
writer.writeheader()
for row in citation:
writer.writerow(row)
outfile.close()
我需要在csv
文件中实现的输出:
ID | SENTENCE | AUTHOR | TITLE | YEAR | URL | VOLUME
--------------------------------------------------------------------------------------------------------------------------------------------------------------
101 | This is a theory sample from a book. | ALEX B.;JOHN K. | BASIC PROGRAMMING | 2010 | an.example.com |
102 | This is a theory from book 1 and book 2 | MARINA E.;STEVE M.;DIANE L. | BE A GOOD PROGRAMMER; I AM AN ENGINEER | 2011; 2013 | | 10
答案 0 :(得分:2)
您可以将dict.get()
方法与字符串格式结合使用。
for i in citation:
authors = [i.get("AUTHOR","")]
titles = [i.get("TITLE","")]
for x in range(1,10):
authors.append(i.get("AUTHOR{}".format(x),""))
titles.append(i.get("TITLE{}".format(x),""))
a_result,t_result = ";".join(a for a in authors if a),"; ".join(t for t in titles if t)
print (a_result+"|"+t_result)
结果:
ALEX B.;JOHN K.|BASIC PROGRAMMING
MARINA E.;STEVE M.;DIANE L.|BE A GOOD PROGRAMMER; I AM AN ENGINEER