Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #103229

Re: downloading a CSV

From Tim Chase <python.list@tim.thechases.com>
Newsgroups comp.lang.python
Subject Re: downloading a CSV
Date 2016-02-19 18:47 -0600
Message-ID <mailman.8.1455929895.13884.python-list@python.org> (permalink)
References <1efa02db-a26a-44bd-a0ea-db81e612ccd6@googlegroups.com>

Show all headers | View raw


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




Back to comp.lang.python | Previous | NextPrevious in thread | Find similar | Unroll thread


Thread

downloading a CSV noydb <jenn.duerr@gmail.com> - 2016-02-19 10:46 -0800
  Re: downloading a CSV MRAB <python@mrabarnett.plus.com> - 2016-02-19 19:24 +0000
  Re: downloading a CSV Grant Edwards <invalid@invalid.invalid> - 2016-02-19 19:43 +0000
  Re: downloading a CSV noydb <jenn.duerr@gmail.com> - 2016-02-19 12:05 -0800
    Re: downloading a CSV Joel Goldstick <joel.goldstick@gmail.com> - 2016-02-19 16:03 -0500
  Re: downloading a CSV Tim Chase <python.list@tim.thechases.com> - 2016-02-19 18:47 -0600

csiph-web