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的新手。请解释一下这个。
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?)发布于 2015-02-23 22:52:17
删除b,您正在以二进制模式打开,因此出现bytes错误:
with open('repo-attributes.csv', newline="") as repofile:您实际上可以删除这两个,因为默认模式是r。
发布于 2015-02-23 22:54:23
您正在rb中打开文件,这意味着read binary请在read模式下打开它。将代码更改为
...
...
with open('repo-attributes.csv', 'r')...
...
...这将以read模式(非二进制)打开文件。
发布于 2015-02-23 22:55:28
跟踪的最后一行告诉我们第一次发生错误的位置。因为您以二进制模式打开文件,所以python正在读取字节。您的文件是csv,以读取模式打开它就足够了。
所以不使用rb,而使用r
https://stackoverflow.com/questions/28676414
复制相似问题