chopping csv-files (newbie)

Dave Cole djc at object-craft.com.au
Thu Apr 18 04:50:44 EDT 2002


> I have a csv-file.. delimited by ;
> example-line from the csv-file
> "90383";"ooooxWOW   90383;";"Some discription";
> "99875";"ooooxSomede90383;0000xNewli99875;";"New line-descriptor";
> 
> In this file there is 3 fields extracted from a database.
> The second field, can vary som 1 to 4 fields separated by ; but not with
> "
> 
> What I need to do is to build an array after opening a given file
> with the information from the second and third fields.
> 
> E.g:
> 
> 90383	Some discription
> 99875	New line-descriptor, Some discription
> 
> You see where I'm going at?
> 
> the second record in this file has a reference to the first record in
> its second field.

You could use my CSV parser:

>>> import csv
>>> p = csv.parser()
>>> p.field_sep = ';'
>>> p.parse('"90383";"ooooxWOW   90383;";"Some discription";')
['90383', 'ooooxWOW   90383;', 'Some discription', '']
>>> p.parse('"99875";"ooooxSomede90383;0000xNewli99875;";"New line-descriptor";')
['99875', 'ooooxSomede90383;0000xNewli99875;', 'New line-descriptor', '']

The module is available at:

        http://www.object-craft.com.au/projects/csv/

- Dave

-- 
http://www.object-craft.com.au



More information about the Python-list mailing list