CSV,Python:正确使用DictWriter(ValueError:dict包含不在字段名称中的字段) [英] CSV, Python: Using DictWriter correctly (ValueError: dict contains fields not in fieldnames)

查看:6315
本文介绍了CSV,Python:正确使用DictWriter(ValueError:dict包含不在字段名称中的字段)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在csv模块(Python 2.7)中,我遇到了困难,无法掌握DictWriter。我有这个(哦,我使用unicodecsv库,因为我看过有问题):

  f = object_instance.return_a_dictionary.keys()
with open('eggs.csv','wb')as csvfile:
spamwriter = unicodecsv.DictWriter(csvfile,fieldnames = f)
spamwriter.writerows (object_instance.return_a_dictionary)

所以我传入我的对象实例。 f是:

  [u'n6s2f0e1',u'n1s0f0e0',u'n2s0f0e1',u'n3s1f0e0',u' n5s2f0e0',u'n4s1f0e1'] 

object_instance.return_a_dictionary是:

  {u'n6s2f0e1':u'stuff',u'n1s0f0e0':u'stuff',u'n2s0f0e1':u'stuff',u'n3s1f0e0 ':u'stuff',u'n5s2f0e0':u'stuff',u'n4s1f0e1':u'stuff'} 

真的我想要第一行:

 东西东西东西东西

我的印象是,writerow通过提供的字典,用提供的dictwriter字段名调用提供的dict的键名,

 回溯(大多数)最近调用最后):
文件< stdin>,第3行,在< module>
文件/usr/lib/python2.7/csv.py,第153行,在作者中
rows.append(self._dict_to_list(rowdict))
>>>文件/usr/lib/python2.7/csv.py,第144行,在_dict_to_list
,.join(wrong_fields))
ValueError:dict包含不在字段名称中的字段:n,6 ,s,2,f,0,e,1

我只是不明白这个点。它与正常的Python csv库和unicode csv库我找到了这一点。

解决方案

您想要 writerow writerows



前者需要一个参数,即要写入的行。后者需要一个可迭代的行。您正在使用字典调用 writerows ,它会尝试遍历字典并写入每个条目。因为迭代过程给出了它们的键,这和调用 writerow(n6s2f0e1)相同,它显然会失败,并显示错误。


I'm having difficulties grasping the DictWriter in the csv module (Python 2.7). I have this (oh, and I'm using a unicodecsv library because I've read there are issues):

f = object_instance.return_a_dictionary.keys()
with open('eggs.csv', 'wb') as csvfile:
    spamwriter = unicodecsv.DictWriter(csvfile, fieldnames=f)
    spamwriter.writerows(object_instance.return_a_dictionary)

So I pass in my object instance. f is:

[u'n6s2f0e1', u'n1s0f0e0', u'n2s0f0e1', u'n3s1f0e0', u'n5s2f0e0', u'n4s1f0e1']

object_instance.return_a_dictionary is:

{u'n6s2f0e1': u'stuff', u'n1s0f0e0': u'stuff', u'n2s0f0e1': u'stuff', u'n3s1f0e0': u'stuff', u'n5s2f0e0': u'stuff', u'n4s1f0e1': u'stuff'}

So really I want a first row:

stuff stuff stuff stuff stuff

I'm under the impression that writerow goes through the provided dictionary, calls the keyname of the provided dict with the dictwriter fieldnames provided, and outputs the value.

Instead I get:

Traceback (most recent call last):
File "<stdin>", line 3, in <module>
File "/usr/lib/python2.7/csv.py", line 153, in writerows
rows.append(self._dict_to_list(rowdict))
>>> File "/usr/lib/python2.7/csv.py", line 144, in _dict_to_list
", ".join(wrong_fields))
ValueError: dict contains fields not in fieldnames: n, 6, s, 2, f, 0, e, 1

I just don't understand this at this point. It does this with both the regular Python csv library and a unicode csv library I've found. Can anyone explain what the issue is?

解决方案

You want writerow not writerows.

The former takes a single argument, which is the row to be written. The latter takes an iterable of rows. You are calling writerows with a dictionary, which tries to iterate over the dictionary and write each entry. Since iterating over dicts gives their keys, this is the same as calling writerow(n6s2f0e1) which (obviously) fails with the error you see.

这篇关于CSV,Python:正确使用DictWriter(ValueError:dict包含不在字段名称中的字段)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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