Downloading multiple files based on info extracted from CSV

Mark Lawrence breamoreboy at yahoo.co.uk
Thu Dec 12 23:19:32 CET 2013


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




More information about the Python-list mailing list