473,406 Members | 2,217 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

extracting numbers from a file, excluding words

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
5 2310
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: lecichy | last post by:
Hello Heres the situation: I got a file with lines like: name:second_name:somenumber:otherinfo etc with different values between colons ( just like passwd file) What I want is to extract...
5
by: Michael Hill | last post by:
Hi, folks. I am writing a Javascript program that accepts (x, y) data pairs from a text box and then analyzes that data in various ways. This is my first time using text area boxes; in the past,...
5
by: dawenliu | last post by:
Hi, I have a file with this content: xxxxxxx xxxxxxxxxx xxxxx xxxxxxx 1 0 0 0 1 1 0 (many more 1's and 0's to follow)
13
by: Kosio | last post by:
Hello, I know of a way to extract digits from a number using the %10 and divide by 10. But I am wondering if there is an algorithm out there that does not use a divide by 10 feature. The...
0
by: Mico | last post by:
I would be very grateful for any help with the following: I currently have the code below. This opens a MS Word document, and uses C#'s internal regular expressions library to find if there is a...
6
by: RSH | last post by:
Hi, I have quite a few .DAT data files that i need to extract the data out of. When i open the files in a text editor I see all of the text that I need to get at BUT there are a lot of junk...
4
by: sofiafig | last post by:
Hi, I have a file with several entries in the form: AFFX-BioB-5_at E. coli /GEN=bioB /gb:J04423.1 NOTE=SIF corresponding to nucleotides 2032-2305 of /gb:J04423.1 DEF=E.coli...
5
by: lim4801 | last post by:
I am currently in doing a program which is given by my tutor: Contemplate that you are working for the phone company and want to sell "special" phone numbers to companies. These phone numbers are...
6
by: Werner | last post by:
Hi, I try to read (and extract) some "self extracting" zipefiles on a Windows system. The standard module zipefile seems not to be able to handle this. False Is there a wrapper or has...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.