By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,742 Members | 1,224 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,742 IT Pros & Developers. It's quick & easy.

extracting numbers from a file, excluding words

P: n/a
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.

Nov 1 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a
1 Nov 2005 09:19:45 -0800, da******@gmail.com <da******@gmail.com>:
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()
Nov 1 '05 #2

P: n/a
Kristina Kudriašova wrote:
1 Nov 2005 09:19:45 -0800, da******@gmail.com <da******@gmail.com>:
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
Nov 1 '05 #3

P: n/a
Steve Horsley wrote:
Kristina Kudriašova wrote:
1 Nov 2005 09:19:45 -0800, da******@gmail.com <da******@gmail.com>:
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.
Nov 1 '05 #4

P: n/a
Kristina Kudriašova <ri******@gmail.com> writes:
1 Nov 2005 09:19:45 -0800, da******@gmail.com <da******@gmail.com>:
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
--
Mike Meyer <mw*@mired.org> http://www.mired.org/home/mwm/
Independent WWW/Perforce/FreeBSD/Unix consultant, email for more information.
Nov 1 '05 #5

P: n/a
On Nov 01, Mike Meyer wrote:
Kristina Kudriašova <ri******@gmail.com> writes:
1 Nov 2005 09:19:45 -0800, da******@gmail.com <da******@gmail.com>:
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())

--
_ _ ___
|V|icah |- lliott http://micah.elliott.name md*@micah.elliott.name
" " """
Nov 1 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.