with open('repo-attributes.csv', 'rb') as repofile:
reader = csv.DictReader(repofile)
for repo in reader:
g.add_vertex(name=repo['repository_url'],
label=repo['repository_url'][19:],
language='(unknown)' if repo['repository_language'] == 'null'
else repo['repository_language'],
watchers=int(repo['repository_watchers']))
这是我的代码。 我收到如下错误。我是python的新手。请好好解释一下。阅读csv文件的python错误
Traceback (most recent call last):
File "C:\Python34\github-network-analysis-master\process.py", line 9, in <module>
for repo in reader:
File "C:\Python34\lib\csv.py", line 109, in __next__
self.fieldnames
File "C:\Python34\lib\csv.py", line 96, in fieldnames
self._fieldnames = next(self.reader)
_csv.Error: iterator should return strings, not bytes (did you open the file in text mode?)
真的错误是自我解释。你用'rb'标志打开了一个文件,这意味着在读取二进制模式下打开。你只需要只读的'r'标志。 – ljetibo 2015-02-23 14:52:58
使用''rb''是运行Python 2时要做的正确事情;在Python 3中,[module documentation](https://docs.python.org/3/library/csv.html)中的建议是使用''r''和'newline ='''。 – 2015-02-23 15:27:45
也许与此处有关:[如何阻止Python的csv.DictWriter.writerows在Windows中的行之间添加空行?](http://stackoverflow.com/q/22361787) – 2015-02-23 15:34:57