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?)
答案 0 :(得分:3)
删除b
,您将以二进制模式打开,因此bytes
错误:
with open('repo-attributes.csv', newline="") as repofile:
您可以将两者都删除,因为默认模式为r
。
答案 1 :(得分:0)
您正在rb
中打开文件,意味着read binary
,请以read
模式打开它。将您的代码更改为
...
...
with open('repo-attributes.csv', 'r')...
...
...
这将以read
模式打开文件(非二进制)。
答案 2 :(得分:0)
跟踪的最后一行说明错误首次发生的位置。 由于您以二进制模式打开文件,因此python正在读取字节。你的文件是csv,在阅读模式下打开它就足够了。
所以代替rb使用r