chopping csv-files (newbie)

Bjørn Ove Grøtan bgrotanLOVELYSPAM at itea.ntnu.no
Thu Apr 18 11:09:04 EDT 2002


Dave Cole wrote:
> 
> > 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:
I'm already using it.. not quite confident on how to use it though
 
> >>> 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', '']

in python.. how do I build a hash-table to get a list like this:

99875	New line-descriptor, Some discription

and so on and so forth...
I need some guidelines on how to get those numbers from the second
field.
The csv-file is sorted so I know that records with 4 id-records comes
last and 1 id-record
comes first.

I thought of making a hash-table with first the 1st-level idnr +
common-name is listed
and when the 2nd level takes the common-name from the idnr already
listet/stored in my hash-table.
then the 3rd and 4th level. How should I build such a hash-table? 

Best regards

Bjørn Ove



More information about the Python-list mailing list