简体   繁体   English

如何将所有字典保存到 CSV 文件?

[英]How can I save all dictionary to a CSV file?

How can I save these dictionaries to CSV?如何将这些字典保存到 CSV?

dict_image dict_image

I want to set headers as "Full Name", "Nick Name", "Height", and "Weight"我想将标题设置为“全名”、“昵称”、“身高”和“体重”

tried this code but it won't work.试过这段代码,但它不会工作。

 with open('my.csv', 'w', encoding='UTF8', newline='') as f: writer = csv.DictWriter(f, fieldnames=fieldnames) writer.writeheader() writer.writerows(dict_name)

enter image description here在此处输入图像描述

I did OCR on your image, and got some sample data.我对您的图像进行了 OCR,并获得了一些示例数据。

import csv

rows = [
    {'Full Name': None,
     'Nick Name': None,
     'profession': 'Model, Actress',
     'Height': 'in centimeters- 165 cm, in meters. 1.65 m, in feet & inches- 5\' 5"',
     'Weight': None},
    {'Full Name': None,
     'Nick Name': 'Chikoo, Run Machine',
     'profession': 'Indian Cricketer (Batsman) ',
     'Height': 'in centimeters- 175 cm, in meters- 1.75 m, in Feet Inches-5\' 9"',
     'Weight': None}
]

with open('my.csv', 'w', newline='') as f:
    writer = csv.DictWriter(f, fieldnames=rows[0].keys())
    writer.writeheader()
    writer.writerows(rows)

When I run that, I get this for my.csv :当我运行它时,我得到了my.csv

Full Name,Nick Name,profession,Height,Weight
,,"Model, Actress","in centimeters- 165 cm, in meters. 1.65 m, in feet & inches- 5' 5""",
,"Chikoo, Run Machine",Indian Cricketer (Batsman) ,"in centimeters- 175 cm, in meters- 1.75 m, in Feet Inches-5' 9""",

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM