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


Groups > comp.lang.python > #61768

Re: Downloading multiple files based on info extracted from CSV

From Mark Lawrence <breamoreboy@yahoo.co.uk>
Subject Re: Downloading multiple files based on info extracted from CSV
Date 2013-12-12 22:19 +0000
References <88346903-2af8-48cd-9829-37cedb717ae5@googlegroups.com>
Newsgroups comp.lang.python
Message-ID <mailman.4036.1386886796.18130.python-list@python.org> (permalink)

Show all headers | View raw


On 12/12/2013 21:43, Matt Graves wrote:
> I have a CSV file containing a bunch of URLs I have to download a file from for clients (Column 7) and the clients names (Column 0) I tried making a script to go down the .csv file and just download each file from column 7, and save the file as [clientname].csv
>
> I am relatively new to python, so this may be way off but…
>
> import urllib
> import csv
> urls = []
> clientname = []

I assume clientnames.

>
> ###This will set column 7 to be a list of urls
> with open('clients.csv', 'r') as f:
>      reader = csv.reader(f)
>      for column in reader:
>          urls.append(column[7])
>
> ###And this will set column 0 as a list of client names
> with open('clients.csv', 'r') as g:
>      reader = csv.reader(g)
>      for column in reader:
>          clientname.append(column[0])

You could do the above in one hit.

with open('clients.csv', 'r') as f:
      reader = csv.reader(f)
      for row in reader:
          urls.append(row[7])
          clientnames.append(row[0])

Note that you're reading rows, not columns.

>
> ###This SHOULD plug in the URL for F, and the client name for G.

What makes you think this, f and g are file handles?

> def downloadFile(urls, clientname):
>      urllib.urlretrieve(f, "%g.csv") % clientname
>

If you want one file at a time you'd want url, clientname.

>
> downloadFile(f,g)

I think you want something like.

for url, clientname in zip(urls, clientnames):
     downloadFile(url, clientname)

>
> When I run it, I get : AttributeError: 'file' object has no attribute 'strip'
>

When you get a traceback like this please cut and paste all it of, not 
just the last line.  Here it seems likely that your call to downloadFile 
doesn't like you passing in the file handle as I've explained above (I 
hope :)

-- 
My fellow Pythonistas, ask not what our language can do for you, ask 
what you can do for our language.

Mark Lawrence

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


Thread

Downloading multiple files based on info extracted from CSV Matt Graves <tunacubes@gmail.com> - 2013-12-12 13:43 -0800
  Re: Downloading multiple files based on info extracted from CSV Mark Lawrence <breamoreboy@yahoo.co.uk> - 2013-12-12 22:19 +0000
  Re: Downloading multiple files based on info extracted from CSV Chris Angelico <rosuav@gmail.com> - 2013-12-13 09:20 +1100
    Re: Downloading multiple files based on info extracted from CSV Matt Graves <tunacubes@gmail.com> - 2013-12-16 06:17 -0800
  Re: Downloading multiple files based on info extracted from CSV John Gordon <gordon@panix.com> - 2013-12-12 22:21 +0000
  Re: Downloading multiple files based on info extracted from CSV Dennis Lee Bieber <wlfraed@ix.netcom.com> - 2013-12-12 20:52 -0500

csiph-web