File Data Extraction Approach

Eddie Corns eddie at holyrood.ed.ac.uk
Fri Nov 23 07:57:02 EST 2001


"Jim St.Cyr" <jstcyr at mediaone.net> writes:

>Hello-

>I have a multiline file of the format:

>: Some Name = data : Another Name = data : etc = data:

>Each line consists of 20 tagnames and associated data.  Some of the tagnames
>have spaces in them though most don't.  There is a space on each side of the
>equal sign and on each side of the colon which acts as a field seperator.  I
>only need the data associated with 7 out of the 20 tagnames.

>I was thinking about removing the whitespace from the line and then seeking
>the tagnames that I am interested in.  This strikes me as sort of brute
>force and I would like some help in formulating an approach that is a bit
>more elegant.

Use a dictionary for sure.

A fairly simple regular expression would convert the line into a list like:
[('Some Name', 'data'), ('Another Name', 'data'), ('etc', 'data')]

eg y=re.findall(r' *([^=]*) = ([^: ]*) *:',line[1:])

Then either a simple map to fill a dictionary or IIRC the newest version
allows you to create a dictionary directly from such a list.

Eddie



More information about the Python-list mailing list