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

Path csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!goblin2!goblin.stu.neva.ru!newsfeed.xs4all.nl!newsfeed2.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail
Return-Path <python-python-list@m.gmane.org>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.001
X-Spam-Evidence '*H*': 1.00; '*S*': 0.00; 'python,': 0.02; 'from:addr:yahoo.co.uk': 0.04; 'attribute': 0.07; 'column': 0.07; 'lawrence': 0.09; 'plug': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'rows,': 0.09; 'subject:files': 0.09; 'def': 0.12; 'assume': 0.14; 'language.': 0.14; 'csv': 0.16; 'received:80.91.229.3': 0.16; 'received:plane.gmane.org': 0.16; 'subject:CSV': 0.16; 'subject:based': 0.16; 'urllib': 0.16; 'language': 0.16; 'wrote:': 0.18; 'passing': 0.19; 'seems': 0.21; 'import': 0.22; 'header:User-Agent:1': 0.23; "i've": 0.25; 'script': 0.25; 'header:X-Complaints-To:1': 0.27; 'header:In- Reply-To:1': 0.27; 'tried': 0.27; "doesn't": 0.30; 'bunch': 0.31; 'explained': 0.31; 'file': 0.32; 'run': 0.32; 'reader': 0.33; 'subject:from': 0.34; 'could': 0.34; 'skip:u 20': 0.35; 'something': 0.35; 'should': 0.36; 'list': 0.37; 'handle': 0.38; 'skip:[ 10': 0.38; 'to:addr:python-list': 0.38; 'to:addr:python.org': 0.39; 'received:org': 0.40; 'skip:u 10': 0.60; 'hope': 0.61; 'new': 0.61; "you're": 0.61; 'save': 0.62; 'making': 0.63; 'name': 0.63; 'our': 0.64; 'relatively': 0.65; 'charset:windows-1252': 0.65; 'here': 0.66; 'containing': 0.69; 'cut': 0.74; 'hit.': 0.84; 'received:89': 0.85
X-Injected-Via-Gmane http://gmane.org/
To python-list@python.org
From Mark Lawrence <breamoreboy@yahoo.co.uk>
Subject Re: Downloading multiple files based on info extracted from CSV
Date Thu, 12 Dec 2013 22:19:32 +0000
References <88346903-2af8-48cd-9829-37cedb717ae5@googlegroups.com>
Mime-Version 1.0
Content-Type text/plain; charset=windows-1252; format=flowed
Content-Transfer-Encoding 8bit
X-Gmane-NNTP-Posting-Host host-89-240-164-174.as13285.net
User-Agent Mozilla/5.0 (Windows NT 6.1; rv:24.0) Gecko/20100101 Thunderbird/24.2.0
In-Reply-To <88346903-2af8-48cd-9829-37cedb717ae5@googlegroups.com>
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.15
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <https://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list/>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <https://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.4036.1386886796.18130.python-list@python.org> (permalink)
Lines 69
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1386886796 news.xs4all.nl 2850 [2001:888:2000:d::a6]:41047
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:61768

Show key headers only | 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