简体   繁体   English

Python使用URL给出错误读取CSV文件

[英]Python reading CSV file using URL giving error

I am trying to read a CSV file using the requests library but I am having issues. 我正在尝试使用请求库读取CSV文件,但是出现问题。

import requests
import csv

url = 'https://storage.googleapis.com/sentiment-analysis-dataset/training_data.csv'
r = requests.get(url)
text = r.iter_lines()
reader = csv.reader(text, delimiter=',')

I then tried 然后我尝试了

for row in reader:
  print(row)

but it gave me this error: 但这给了我这个错误:

Error: iterator should return strings, not bytes (did you open the file in text mode?)

How should I fix this? 我该如何解决?

What you probably want is: 您可能想要的是:

text = r.iter_lines(decode_unicode=True)

This will return a strings -iterator instead of a bytes -iterator. 这将返回strings -iterator而不是bytes -iterator。 (See here for documentation.) (有关文档,请参见此处 。)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM