Omar Murcia
Omar Murcia

Reputation: 567

How to print a csv content from remote url using python 3.x?

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.

Upvotes: 1

Views: 2820

Answers (2)

Mika72
Mika72

Reputation: 411

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(','))

Upvotes: 3

Mathew Savage
Mathew Savage

Reputation: 168

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

Upvotes: 3

Related Questions