Python 从网站获取 csv 数据

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15537245/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 20:21:37  来源:igfitidea点击:

get csv data from a website

pythoncsvpython-3.x

提问by user2117875

How do I download and read the CSV data on thiswebsite using Python:

如何使用 Python下载和读取网站上的 CSV 数据:

http://earthquake.usgs.gov/earthquakes/feed/csv/1.0/hour

http://earthquake.usgs.gov/earthquakes/feed/csv/1.0/hour

采纳答案by eandersson

It depends on what you want to do with the data. If you simply want to download the data you can use urllib2.

这取决于您想对数据做什么。如果您只想下载数据,可以使用urllib2

import urllib2

downloaded_data  = urllib2.urlopen('http://...')

for line in downloaded_data.readlines():
    print line

If you need to parse the csvyou can use the urrlib2and csvmodules.

如果您需要解析csv,您可以使用urrlib2csv模块。

Python 2.X

蟒蛇2.X

import csv
import urllib2

downloaded_data  = urllib2.urlopen('http://...')
csv_data = csv.reader(downloaded_data)

for row in csv_data:
    print row

Python 3.X

蟒蛇 3.X

import csv
import urllib.request

downloaded_data  = urllib.request.urlopen('http://...')
csv_data = csv.reader(downloaded_data)

for row in csv_data:
    print(row)