python 如何将很多个有相同键但值不同的字典写入csv或txt文件中,一个字典占一行
例如,有很多个字典{"nickname":"abc","content":"Iamastudet."},{"nickname":"123","content":"Iwan...
例如,有很多个字典{"nickname":"abc","content":"I am a studet."},{"nickname":"123","content":"I want to eat."}...{...},都有相同的键值,但是值不相同,怎么样能把这些都写入到csv文件(文本文件也可以)中,所有nickname一列,对应的content一列。格式为:nickname content
abc I am a student.
123 I want to eat.
... ... 展开
abc I am a student.
123 I want to eat.
... ... 展开
展开全部
l = [{"nickname":"abc","content":"I am a studet."},{"nickname":"123","content":"I want to eat."}]
with open('d://111.txt','w') as file:
for d in l:
file.write('%s,%s\n'%(d['nickname'],d['content']))
效果:
abc,I am a studet.
123,I want to eat.
csv就是逗号分隔的文本文件
with open('d://111.txt','w') as file:
for d in l:
file.write('%s,%s\n'%(d['nickname'],d['content']))
效果:
abc,I am a studet.
123,I want to eat.
csv就是逗号分隔的文本文件
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询