Search code examples
pythonpython-3.xcsvurllib3

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.


Solution

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