如何使用csv DictWriter将列表中的信息写入csv文件? [英] How to write information from a list to a csv file using csv DictWriter?

查看:882
本文介绍了如何使用csv DictWriter将列表中的信息写入csv文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用 csv 模块,我一直试图将列表中的信息传递/写入 python 使用独裁者并收到一个奇怪的错误,女巫将处于实际代码之下。我想知道是什么,代码中的错误,以及如何解决它。

Using the csv module , Ive been trying to pass / write information from a list to a csv file in pythonusing the dictwriter and im getting a strange error , witch will be under the actual code. I want to know whats , the error in the code , and how can we fix it.

import csv 

names = ['kisha' ,'smith'  , 'kishasmith@gmail.com', 40000  ,  '1-1-2029'   ,'janitor' ]

with open('employees.csv' , 'w')as employee_file:
     fieldnames2 = ['first' , 'last' , 'email' , 'salary' , 'DOB' , 'occupation']
     csvwriter = csv.DictWriter(employee_file , fieldnames = fieldnames2 , delimiter = ',')
     csvwriter.writeheader()
     for info in names:
        csvwriter.writerow(info)

我还看到了与此主题相关的其他问题,但尼姑适用于我。

Ive seen other questions related to this topic but nun are applicable to me.

Traceback (most recent call last):
  File "C:\Users\Kxrk\Downloads\Codes\Employee project\new.py", line 10, in <module>
    csvwriter.writerow(info)
  File "C:\Users\Kxrk\AppData\Local\Programs\Python\Python36-32\lib\csv.py", line 155, in writerow
    return self.writer.writerow(self._dict_to_list(rowdict))
  File "C:\Users\Kxrk\AppData\Local\Programs\Python\Python36-32\lib\csv.py", line 148, in _dict_to_list
    wrong_fields = rowdict.keys() - self.fieldnames
AttributeError: 'str' object has no attribute 'keys'
[Finished in 0.1s]


推荐答案

< csv.DictWriter 类的p> writerow()函数期望参数为 dict ,而您正在将字符串传递给它。 csv.DictWriter 文档,其中说:

writerow() function of the csv.DictWriter class expects the parameter as dict, whereas you are passing string to it. It is clearly mentioned in csv.DictWriter document which says:


创建一个对象,其功能类似于常规编写器,但映射了
字典到输出行上。 fieldnames参数是键序列
,用于标识字典
中传递给 writerow()方法的值写入csvfile的顺序。

Create an object which operates like a regular writer but maps dictionaries onto output rows. The fieldnames parameter is a sequence of keys that identify the order in which values in the dictionary passed to the writerow() method are written to the csvfile.

为了使其工作,请传递 dict 对象(带有映射csv标头和相应的列值之间)。例如:

In order to make it work, pass the dict object (with the mapping between the csv headers and the corresponding column value). For example:

import csv

names = ['kisha' ,'smith'  , 'kishasmith@gmail.com', 40000  ,  '1-1-2029'   ,'janitor' ]
fieldnames2 = ['first' , 'last' , 'email' , 'salary' , 'DOB' , 'occupation']

# for creating the dictionary object mapping "names" and "fieldnames2"
my_names_dict = dict(zip(fieldnames2, names))

with open('/path/to/my_file.csv' , 'w')as employee_file:
     csvwriter = csv.DictWriter(employee_file , fieldnames = fieldnames2 , delimiter = ',')
     csvwriter.writeheader()
     csvwriter.writerow(my_names_dict)

以上代码将创建文件 /路径/到/my_file.csv ,内容为:

Above code will create a file /path/to/my_file.csv with the content as:

first,last,email,salary,DOB,occupation
kisha,smith,kishasmith@gmail.com,40000,1-1-2029,janitor

这篇关于如何使用csv DictWriter将列表中的信息写入csv文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆