File Data Extraction Approach 
Author Message
 File Data Extraction Approach

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.

Thank you.

Jim



Tue, 11 May 2004 09:08:23 GMT  
 File Data Extraction Approach

Quote:

> 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.

Just write code that reads *all* the tags and data from a line, then
ignore the ones you don't want.  Do the work once the data is in a
more tractable form (i.e. in memory) rather than trying to dissect
the file while it's still a file.

Slightly ugly code which makes a dictionary from each line and prints
it to stdout:

Quote:
>>> f = open('file')
>>> import string
>>> while line in f.readlines():

...   d = {}
...   for field in line.split(':'):
...     if field.strip():
...       tag, data = map(string.strip, field.split('='))
...       d[tag] = data
...   print 'Result %s' % d

--
----------------------
Peter Hansen, P.Eng.



Tue, 11 May 2004 10:33:55 GMT  
 File Data Extraction Approach

Quote:

>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



Tue, 11 May 2004 20:57:02 GMT  
 
 [ 3 post ] 

 Relevant Pages 

1. data extraction from screen or text file

2. tps data extraction or data recovery

3. Data Extraction

4. Extraction of data from Clarion 2.1 for DOS to Access 97

5. Help please/Data extraction from Lotus for clipper

6. Server-client-development / Data-extraction without databaseaccess

7. Read VFP data files into Clipper data files

8. I have 100 data files, I want to join them together as one data file

9. Unix Data files vs DOS data files

10. non-gridded ASCII data file to netCDF data file

11. figures extraction in a postcript file

12. Image extraction without IMAQ, using Read JPEG file.vi

 

 
Powered by phpBB® Forum Software