Python CSV没有属性'Writer' [英] Python CSV Has No Attribute 'Writer'

查看:3471
本文介绍了Python CSV没有属性'Writer'的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有一点代码给我麻烦。

There's a bit of code giving me trouble. It was working great in another script I had but I must have messed it up somehow.

如果csv

import csv

if csv:
    with open('output.csv', 'wb') as csvfile:
        csvout = csv.writer(csvfile, delimiter=',',
            quotechar=',', quoting=csv.QUOTE_MINIMAL)
        csvout.writerow(['A', 'B', 'C'])
        csvfile.close()

给我:

Traceback (most recent call last):
  File "import csv.py", line 34, in <module>
    csvout = csv.writer(csvfile, delimiter=',',
AttributeError: 'str' object has no attribute 'writer'

如果我删除 if语句,我会得到:

If I remove the if statement, I get:

Traceback (most recent call last):
  File "C:\import csv.py", line 34, in <module>
    csvout = csv.writer(csvfile, delimiter=',',
AttributeError: 'NoneType' object has no attribute 'writer'

我做错了什么愚蠢的东西,我试着改变文件名称像test.py,因为我看到在另一个SO post,没有工作。

What silly thing am I doing wrong? I did try changing the file name to things like test.py as I saw that in another SO post, didn't work.

推荐答案

如果你设置了分配给 csv (看起来像一个字符串)的东西,因此,最简单的事情是改变任何分配给 csv 这不是模块,并调用其他的东西...

If you've set something that assigns to csv (looks like a string) then you're shadowing the module import. So, the simplest thing is to just change whatever's assigning to csv that isn't the module and call it something else...

实际上发生的是:

import csv
csv = 'bob'
csvout = csv.writer(somefile)

删除 csv 并从那里去...

Remove the further assignment to csv and go from there...

这篇关于Python CSV没有属性'Writer'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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