extracting numbers from a file, excluding fixed words

2005-10-29 Thread dawenliu
Hi, I have a file with this content:
xxx xx x xxx
1
0
0
0
1
1
0
(many more 1's and 0's to follow)
y yy yyy yy y yyy

The x's and y's are FIXED and known words which I will ignore, such as
"This is the start of the file" and "This is the end of the file".  The
digits 1 and 0 have UNKNOWN length.  I want to extract the digits and
store them in a file.  Any suggestions will be appreciated.

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: extracting numbers from a file, excluding fixed words

2005-10-29 Thread dawenliu
Thanks Kent.  The code looks reasonable, but the result is that, the
output file comes out identical as the input file, with all the 
and  remaining inside.

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: extracting numbers from a file, excluding fixed words

2005-10-29 Thread dawenliu
I've changed the code a little bit and works fine now:
inf = open('input.txt')
out = open('output.txt', 'w')

skips = [
'xxx xx x xxx',
  'y yy yyy yy y yyy']

for line in inf:
  flag = 0
  for skip in skips:
if skip in line:
  flag = 1
  continue
  if flag == 0:
out.write(line)

inf.close()
out.close()

-- 
http://mail.python.org/mailman/listinfo/python-list


how to discard a line if it's not a number?

2005-10-29 Thread dawenliu
Hi, I'm reading a file line by line, and whenever a line is not
consisted of a single number (such as 0.315), I want to discard that
line (and store only the single numbers).

For example,

0.315
discarded this line of text
3.8
-1.44
forget about me also
2.6


Then I want to store only the four numbers into another file, without
the two sentences.  
Suggestions are appreciated!

-- 
http://mail.python.org/mailman/listinfo/python-list


extracting numbers from a file, excluding words

2005-11-01 Thread dawenliu
Hi, I have a file with this content:

 z zzz z
...
xxx xx x 34.215
zzz zz 
...

"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 "xxx xx x " is FIXED and KNOWN. The "z"
sentences are can vary.  There are also unknown number of "z" lines.

Any suggestions will be appreciated.

-- 
http://mail.python.org/mailman/listinfo/python-list