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