[英]How do I write this dictionary of lists to a csv file in this format
I want to write this dictionary of lists to a csv file in this format using csv: 我想使用csv以这种格式将列表的字典写入csv文件:
Ola, elizabethy, beale/cambray,st_hildas
Evie, st_hildas,beale/cambray,elizabethn
Lorna, beale/cambray,elizabethn,st_hildas
So that I can eventually write it to an excel file that appears like this: 这样我最终可以将其写入一个如下所示的excel文件中:
Ola elizabethy beale/cambray st_hildas
Evie st_hildas beale/cambray elizabethn
Lorna beale/cambray elizabethn st_hildas
This is the way it looks in python. 这就是它在python中的样子。
students = {"Ola":["elizabethy","beale/cambray","st_hildas"],"Evie":["st_hildas","beale/cambray","elizabethn"],"Lorna":["beale/cambray","elizabethn","st_hildas"],"Morgan":["st_hildas","beale/cambray","elizabethy"],"Ola1":["beale/cambray","elizabethn","st_hildas"],"Lorna1":["elizabethn","beale/cambray","st_hildas"],"Lorna2":["beale/cambray","elizabethn","st_hildas"],"Evie1":["st_hildas","beale/cambray","elizabethy"],"Evie3":["st_hildas","beale/cambray","elizabethn"]}
import csv
students = {"Ola":["elizabethy","beale/cambray","st_hildas"],"Evie":["st_hildas","beale/cambray","elizabethn"],"Lorna":["beale/cambray","elizabethn","st_hildas"],"Morgan":["st_hildas","beale/cambray","elizabethy"],"Ola1":["beale/cambray","elizabethn","st_hildas"],"Lorna1":["elizabethn","beale/cambray","st_hildas"],"Lorna2":["beale/cambray","elizabethn","st_hildas"],"Evie1":["st_hildas","beale/cambray","elizabethy"],"Evie3":["st_hildas","beale/cambray","elizabethn"]}
mylist=[[k]+v for k,v in students.items()]
with open("mylist.csv", 'w', newline='') as myfile:
wr = csv.writer(myfile, quoting=csv.QUOTE_ALL)
wr.writerows(mylist)
To write to CSV (without importing any libraries) 要写入CSV(不导入任何库)
students = {
"Ola":["elizabethy","beale/cambray","st_hildas"],
"Evie":["st_hildas","beale/cambray","elizabethn"],
"Lorna":["beale/cambray","elizabethn","st_hildas"],
"Morgan":["st_hildas","beale/cambray","elizabethy"],
"Ola1":["beale/cambray","elizabethn","st_hildas"],
"Lorna1":["elizabethn","beale/cambray","st_hildas"],
"Lorna2":["beale/cambray","elizabethn","st_hildas"],
"Evie1":["st_hildas","beale/cambray","elizabethy"],
"Evie3":["st_hildas","beale/cambray","elizabethn"]}
with open('test.csv', 'w') as f:
for key in students.keys():
f.write("%s, %s\n"%(key,','.join(students[key])))
To read the CSV back into a dictionary: 要将CSV重新读回字典中:
students1 = dict()
with open('test.csv', 'r') as f:
for line in f:
key, value = line.strip().split(', ')
students1[key] = value.split(',')
import pandas as pd
d = {"Ola":["elizabethy","beale/cambray","st_hildas"],"Evie":["st_hildas","beale/cambray","elizabethn"],"Lorna":["beale/cambray","elizabethn","st_hildas"],"Morgan":["st_hildas","beale/cambray","elizabethy"],"Ola1":["beale/cambray","elizabethn","st_hildas"],"Lorna1":["elizabethn","beale/cambray","st_hildas"],"Lorna2":["beale/cambray","elizabethn","st_hildas"],"Evie1":["st_hildas","beale/cambray","elizabethy"],"Evie3":["st_hildas","beale/cambray","elizabethn"]}
df = pd.DataFrame.from_dict(d)
df.T.to_csv("test.csv")
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.