如何使用Python从URL读取CSV文件?

时间:2013-04-29 16:36:43

标签: python csv curl output python-2.x

当我卷曲到API调用链接http://example.com/passkey=wedsmdjsjmdd

curl 'http://example.com/passkey=wedsmdjsjmdd'

我以csv文件格式获取员工输出数据,例如:

"Steve","421","0","421","2","","","","","","","","","421","0","421","2"

如何使用python解析这个。

我试过了:

import csv 
cr = csv.reader(open('http://example.com/passkey=wedsmdjsjmdd',"rb"))
for row in cr:
    print row

但它没有用,我收到了错误

http://example.com/passkey=wedsmdjsjmdd No such file or directory:

谢谢!

8 个答案:

答案 0 :(得分:62)

您需要将open替换为urllib.urlopenurllib2.urlopen

e.g。

import csv
import urllib2

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

for row in cr:
    print row

这会输出以下内容

Year,City,Sport,Discipline,NOC,Event,Event gender,Medal
1924,Chamonix,Skating,Figure skating,AUT,individual,M,Silver
1924,Chamonix,Skating,Figure skating,AUT,individual,W,Gold
...

答案 1 :(得分:59)

使用pandas直接从URL

读取csv文件非常简单
import pandas as pd
data = pd.read_csv('https://example.com/passkey=wedsmdjsjmdd')

这将以表格格式读取您的数据,这将非常容易处理

答案 2 :(得分:15)

您也可以使用请求模块执行此操作:

url = 'http://winterolympicsmedals.com/medals.csv'
r = requests.get(url)
text = r.iter_lines()
reader = csv.reader(text, delimiter=',')

答案 3 :(得分:12)

要在下载大文件时提高性能,下面的工作可能会更有效:

import requests
from contextlib import closing
import csv

url = "http://download-and-process-csv-efficiently/python.csv"

with closing(requests.get(url, stream=True)) as r:
    reader = csv.reader(r.iter_lines(), delimiter=',', quotechar='"')
    for row in reader:
        # Handle each row here...
        print row   

通过在GET请求中设置stream=True,当我们将r.iter_lines()传递给csv.reader()时,我们将generator传递给csv.reader()。通过这样做,我们启用csv.reader()以for row in reader懒惰地遍历响应中的每一行。

这可以避免在我们开始处理之前将整个文件加载到内存中,从而大大减少大文件的内存开销。

答案 4 :(得分:7)

该问题被标记为python-2.x,因此篡改原始问题或已接受的答案似乎不合适。但是,现在不支持Python 2,并且此问题对于“ python csv urllib”仍然具有很好的google功能,因此这里是更新的Python 3解决方案。

现在有必要将urlopen的响应(以字节为单位)解码为有效的本地编码,因此accepted answer必须稍作修改:

import csv, urllib.request

url = 'http://winterolympicsmedals.com/medals.csv'
response = urllib.request.urlopen(url)
lines = [l.decode('utf-8') for l in response.readlines()]
cr = csv.reader(lines)

for row in cr:
    print(row)

请注意,以lines =开头的额外行是urlopen现在在urllib.request模块中,而print当然需要括号的事实。

几乎没有广告,但是是的,csv.reader 可以从字符串列表中读取。

而且由于其他人提到了熊猫,因此这里有一种方法可以在控制台友好的输出中显示CSV:

python3 -c 'import pandas
df = pandas.read_csv("http://winterolympicsmedals.com/medals.csv")
print(df.to_string())'

(是,这是三行,但是您可以将其复制粘贴为一个命令。;)

答案 5 :(得分:1)

import pandas as pd
url='https://raw.githubusercontent.com/juliencohensolal/BankMarketing/master/rawData/bank-additional-full.csv'
data = pd.read_csv(url,sep=";") # use sep="," for coma separation. 
data.describe()

enter image description here

答案 6 :(得分:1)

您试图使用curl命令将文件下载到本地硬盘(HD)。但是,您需要在HD上指定路径

curl http://example.com/passkey=wedsmdjsjmdd -o ./example.csv
cr = csv.reader(open('./example.csv',"r"))
for row in cr:
    print row



答案 7 :(得分:0)

我也在csv文件(Python 3.6.9)中使用这种方法:

import csv
import io
import requests

r = requests.get(url)
buff = io.StringIO(r.text)
dr = csv.DictReader(buff)
for row in dr:
    print(row)