1

I want to print csv content from remote url, but I get this:

Error Traceback (most recent call last) in () ----> 1 for row in cr: 2 print(row)

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

My code is:

import csv
import urllib3

medals_url = "http://winterolympicsmedals.com/medals.csv"
http = urllib3.PoolManager()
r = http.request("GET", medals_url)
r.status
response = r.data
cr = csv.reader(response)
for row in cr:
    print(row)

Thanks in advance.

Omar Murcia
  • 547
  • 2
  • 11
  • 26
  • Possible duplicate of [csv.Error: iterator should return strings, not bytes](https://stackoverflow.com/questions/8515053/csv-error-iterator-should-return-strings-not-bytes) – nutic May 05 '18 at 08:58
  • 1
    try changing `cr = csv.reader(response)` to `cr = csv.reader(response.decode('utf-8'))`, note: this is assuming the byte stream is utf-8 encoded. – Jack Homan May 05 '18 at 09:01
  • 1
    I would have a look here https://stackoverflow.com/questions/32400867/pandas-read-csv-from-url this will put your data in a useful structure, so you can do something with it – Mathew Savage May 05 '18 at 09:01
  • @MathewSavage You are right! Thank you so much!. – Omar Murcia May 05 '18 at 09:04

2 Answers2

3

This can be done directly with Pandas, which will put your data in a useful format for processing

import pandas as pd

df = pd.read_csv('http://winterolympicsmedals.com/medals.csv')
Mathew Savage
  • 168
  • 11
3

This might help.

import urllib3

medals_url = "http://winterolympicsmedals.com/medals.csv"
http = urllib3.PoolManager()
r = http.request("GET", medals_url)
r.status

data = "".join(map(chr,r.data))
print(data)
data = data.split('\n')
for row in data:
    print(row) # or print(row.split(','))
Mika72
  • 413
  • 2
  • 12