[Csv] read a csv file from StringIO

Pitam Mitra pitam at deepspace.ucsb.edu
Tue Aug 31 02:59:41 CEST 2010


Hi,

I am trying to read a CSV file from memory. Basically, what
I am trying to do is - I am downloading a CSV file from a
web site, and trying to parse it through CSV module,
without saving it to disk. However, CSV behaves in a
different way. For example:

import csv, urllib

url="http://......./xxxxx.csv"
webFile = urllib.urlopen(url)
localFile = open(fname, 'w')
localFile.write(webFile.read())
webFile.close()
localFile.close()

csvReader=csv.reader(open(fname))
for row in csvReader:
    print row


#gives output as: 
#['-2.0000e-03', '-0.0002']
#.......


Same thing, slightly different:

import csv, urllib

url="http://......./xxxxx.csv"
webFile = urllib.urlopen(url)

csvReader=csv.reader(open(webFile.read()))
webFile.close()
for row in csvReader:
    print row


#gives output:
#['-']
#....

How can I get rid of this behaviour?


Pitam


More information about the Csv mailing list