gpt4 book ai didi

python 读取 csv 文件时出错

转载 作者:行者123 更新时间:2023-11-30 23:14:35 27 4
gpt4 key购买 nike

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?)

最佳答案

删除 b,您将以二进制模式打开,因此出现 bytes 错误:

 with open('repo-attributes.csv', newline="") as repofile:

您实际上可以删除两者,因为默认模式是 r

关于python 读取 csv 文件时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28676414/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com