extracting numbers from a file, excluding words

D

dawenliu

Hi, I have a file with this content:

zzzz zzzzz zzz zzzzz
....
xxxxxxx xxxxxxxxxx xxxxx 34.215
zzzzzzz zz zzzz
....

"x" and "z" are letters.
The lines with "z" are trash, and only the lines with "x" are
important. I want to extract the number (34.215 in this case) behind
the letters x, and store it in a file.

The sentence "xxxxxxx xxxxxxxxxx xxxxx " is FIXED and KNOWN. The "z"
sentences are can vary. There are also unknown number of "z" lines.

Any suggestions will be appreciated.
 
?

=?ISO-8859-13?Q?Kristina_Kudria=F0ova?=

1 Nov 2005 09:19:45 -0800 said:
Hi, I have a file with this content:

zzzz zzzzz zzz zzzzz
...
xxxxxxx xxxxxxxxxx xxxxx 34.215
zzzzzzz zz zzzz
...

Hi,

I'd suggest doing this:

f = file('...')
for line in f:
if 'xxxxxxx xxxxxxxxxx xxxxx' in line:
var = float(line[len('xxxxxxx xxxxxxxxxx xxxxx'):].strip())
f.close()
 
S

Steve Horsley

Kristina said:
1 Nov 2005 09:19:45 -0800 said:
Hi, I have a file with this content:

zzzz zzzzz zzz zzzzz
...
xxxxxxx xxxxxxxxxx xxxxx 34.215
zzzzzzz zz zzzz
...

Hi,

I'd suggest doing this:

f = file('...')
for line in f:
if 'xxxxxxx xxxxxxxxxx xxxxx' in line:
var = float(line[len('xxxxxxx xxxxxxxxxx xxxxx'):].strip())
f.close()

I think I prefer "if line.startswith('xxxxxxx xxxxxxxxxx
xxxxx'):" . Feels cleaner to me.

Steve
 
J

Jeffrey Schwab

Steve said:
Kristina said:
1 Nov 2005 09:19:45 -0800 said:
Hi, I have a file with this content:

zzzz zzzzz zzz zzzzz
...
xxxxxxx xxxxxxxxxx xxxxx 34.215
zzzzzzz zz zzzz
...

Hi,

I'd suggest doing this:

f = file('...')
for line in f:
if 'xxxxxxx xxxxxxxxxx xxxxx' in line:
var = float(line[len('xxxxxxx xxxxxxxxxx xxxxx'):].strip())
f.close()


I think I prefer "if line.startswith('xxxxxxx xxxxxxxxxx xxxxx'):" .
Feels cleaner to me.

Especially if any "z" lines might include the magic pattern.
 
M

Mike Meyer

Kristina Kudriaðova said:
1 Nov 2005 09:19:45 -0800 said:
Hi, I have a file with this content:

zzzz zzzzz zzz zzzzz
...
xxxxxxx xxxxxxxxxx xxxxx 34.215
zzzzzzz zz zzzz
...

Hi,

I'd suggest doing this:

f = file('...')
for line in f:
if 'xxxxxxx xxxxxxxxxx xxxxx' in line:
var = float(line[len('xxxxxxx xxxxxxxxxx xxxxx'):].strip())
f.close()

Alternatively:

start = len('xxxxxxx xxxxxxxxxx xxxxx')
for line in f:
if line.startswith('xxxxxxx xxxxxxxxxx xxxxx'):
var = float(line[start:].strip())

<mike
 
M

Micah Elliott

Kristina Kudriaðova said:
1 Nov 2005 09:19:45 -0800 said:
Hi, I have a file with this content:

zzzz zzzzz zzz zzzzz
...
xxxxxxx xxxxxxxxxx xxxxx 34.215
zzzzzzz zz zzzz
...

Hi,

I'd suggest doing this:

f = file('...')
for line in f:
if 'xxxxxxx xxxxxxxxxx xxxxx' in line:
var = float(line[len('xxxxxxx xxxxxxxxxx xxxxx'):].strip())
f.close()

Alternatively:

start = len('xxxxxxx xxxxxxxxxx xxxxx')
for line in f:
if line.startswith('xxxxxxx xxxxxxxxxx xxxxx'):
var = float(line[start:].strip())

To refine this even further, I'll add that 'xxx...' is an ugly pattern
to repeat, and prone to mistyping, so add a tempvar and apply DRY::

pattern = 'xxxxxxx xxxxxxxxxx xxxxx'
start = len(pattern)
for line in f:
if line.startswith(pattern):
var = float(line[start:].strip())
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,042
Latest member
icassiem

Latest Threads

Top