downloading a CSV

Tim Chase python.list at tim.thechases.com
Fri Feb 19 19:47:59 EST 2016


On 2016-02-19 10:46, noydb wrote:
> I want to be able to download this CSV file and save to disk
> >> http://earthquake.usgs.gov/earthquakes/feed/v1.0/summary/all_month.csv 

  from urllib.request import urlopen
  data = urlopen("http://earthquake.usgs.gov/earthquakes/feed/v1.0/summary/all_month.csv")

> This (interacting with the web using python) is very new to me, so
> can anyone provide direction on how to go about doing this?  Is it
> involved?  Is there a good example out there that can illustrate
> the needs/mechanics/how-to?

You can then either iterate over the incoming data and write it to a
file:

  with open('output.csv', 'wb') as output:
    output.writelines(data)

or you can process it as it comes in:

  import csv
  r = csv.DictReader(line.decode('utf8') for line in data)
  for i, row in enumerate(r):
    do_something(row)
    if i == 0: print(repr(r))

{'rms': '', 'gap': '158.52', 'latitude': '38.3625', 'magType': 'ml',
'net': 'nn', 'horizontalError': '', 'status': 'automatic', 'depth':
'3', 'nst': '5', 'magError': '', 'magSource': 'nn', 'depthError': '',
'longitude': '-118.4693', 'updated': '2016-02-20T00:17:41.069Z',
'mag': '1.1', 'type': 'earthquake', 'time':
'2016-02-20T00:15:24.120Z', 'id': 'nn00532282', 'locationSource':
'nn', 'dmin': '0.128', 'magNst': '', 'place': '22km SE of Hawthorne,
Nevada'}



-tkc








More information about the Python-list mailing list