0

I am trying to read a CSV file using the requests library but I am having issues.

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?

1 Answer 1

1

What you probably want is:

text = r.iter_lines(decode_unicode=True)

This will return a strings-iterator instead of a bytes-iterator. (See here for documentation.)

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.