Question on the "csv" library

Mark Lawrence breamoreboy at yahoo.co.uk
Fri Aug 28 19:14:36 CEST 2009


vsoler wrote:
> On Aug 28, 5:43 pm, Steven Rumbalski <googleacco... at rumbalski.com>
> wrote:
>> On Aug 27, 3:06 pm, vsoler <vicente.so... at gmail.com> wrote:
>>
>>
>>
>>> I am trying to read a csv file generated by excel.
>>> Although I succeed in reading the file, the format that I get is not
>>> suitable for me.
>>> I've done:
>>>>>> import csv
>>>>>> spamReader = csv.reader(open('C:\\abc.csv', 'r'))
>>>>>> print spamReader
>>> <_csv.reader object at 0x01022E70>
>>>>>> for row in spamReader:
>>>         print row
>>> ['codigo;nombre;cantidad']
>>> ['a;qwe;1']
>>> ['b;asd;2']
>>> ['c;zxc;3']
>>> My questions are:
>>> 1- Why using "print spamReader" I cannot see the data?
>>>     I expected to see a list of lists, a kind of a matrix, but I get
>>> nothing
>>> 2- Why are the rows in a single string?
>>>    I expected a list of fields that, when text, would be delimited by
>>> "
>>>   To tell the truth, the file generated by excel does not contain the
>>> strings delimited by ". Isn't it weird?
>>> 3- Is there anything I can do to have my data in a list of lists
>>> structure? would another kind of data suit better my needs?
>>> Thank you for your help
>>> Vicente Soler
>> the csv module can handle any delimiter.
>>
>> change this >>> spamReader = csv.reader(open('C:\\abc.csv', 'r'))
>> to this >>> spamReader = csv.reader(open('C:\\abc.csv', 'r'),
>> delimiter=';')
>>
>> hope this helps,
>> Steven Rumbalski
> 
> Thank you very much for all your comments. After reading them I can
> conclude that:
> 
> 1- the CSV format is not standardized; each piece of software uses it
> differently
> 
> 2- the "C" in "CSV" does not mean "comma" for Microsoft Excel; the ";"
> comes from my regional Spanish settings
> 
> 3- Excel does not even put quotes around litteral texts, not even when
> the text contains a blank
> 
> But, perhaps, there is no standard alternative to CSV !!!
This depends on the use case of yourself or your users.  If you could 
give more detail on what you are trying to achieve then I'm sure that 
more help will be forthcoming.  For example, could the file be saved in
Excel 97-2003 xls format and then processed with the excellent 
xlrd/xlwt/xlutils? They are available here:-
http://pypi.python.org/pypi/xlutils/1.4.0

-- 
Kindest regards.

Mark Lawrence.




More information about the Python-list mailing list