How to open .csv file from a url with Python?

11,408

Solution 1

Download the stream, then process:

import urllib2
url = "http://httpbin.org/get"
response = urllib2.urlopen(url)
data = response.read()
read = csv.DictReader(data)

Solution 2

I recommend pandas for this:

import pandas as pd
read = pandas.io.parsers.read_csv("http://....", ...)

please see the documentation.

Solution 3

You can do the following :

import csv
import urllib2

url = 'http://winterolympicsmedals.com/medals.csv'
response = urllib2.urlopen(url)
cr = csv.reader(response)

for row in cr:
 print row
Share:
11,408
Admin
Author by

Admin

Updated on June 14, 2022

Comments

  • Admin
    Admin almost 2 years

    I'm trying to open a csv file from a url but for some reason I get an error saying that there is an invalid mode or filename. I'm not sure what the issue is. Help?

    url = "http://...."
    data = open(url, "r")
    read = csv.DictReader(data)