468,301 Members | 1,531 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,301 developers. It's quick & easy.

extracting numbers from a file, excluding fixed words

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)
yyyyy yyyyyy yyy yyyyyy yyyyy 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.

Oct 29 '05 #1
5 1570
dawenliu wrote:
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)
yyyyy yyyyyy yyy yyyyyy yyyyy 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.


Off the top of my head (not tested):

inf = open('input.txt')
out = open('output.txt', 'w')

skips = [
'xxxxxxx xxxxxxxxxx xxxxx xxxxxxx',
'yyyyy yyyyyy yyy yyyyyy yyyyy yyy',
]

for line in inf:
for skip in skips:
if skip in line:
continue
out.write(line)

inf.close()
out.close()

Kent
Oct 29 '05 #2
dawenliu wrote:
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)
yyyyy yyyyyy yyy yyyyyy yyyyy 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.


Open the file and read each line 1 at a time. If the line doesn't equal
x or y then add the line to a list

f = open("file.txt")

numbers = []

for eachline in f.xreadlines():
if (eachline <> x) or (eachline <> y):
numbers.append(eachline)

Oct 29 '05 #3
Thanks Kent. The code looks reasonable, but the result is that, the
output file comes out identical as the input file, with all the xxxx
and yyyy remaining inside.

Oct 29 '05 #4
I've changed the code a little bit and works fine now:
inf = open('input.txt')
out = open('output.txt', 'w')

skips = [
'xxxxxxx xxxxxxxxxx xxxxx xxxxxxx',
'yyyyy yyyyyy yyy yyyyyy yyyyy 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()

Oct 29 '05 #5
dawenliu <da******@gmail.com> wrote:
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)
yyyyy yyyyyy yyy yyyyyy yyyyy 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.


[[warning, untested code...]]

infile = open('infile.txt')
oufile = open('oufile.txt', 'w')
for line in infile:
if line.strip().isdigit(): oufile.write(line)
oufile.close()
infile.close()
Alex
Oct 29 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

4 posts views Thread by lecichy | last post: by
2 posts views Thread by Aleander | last post: by
5 posts views Thread by dawenliu | last post: by
13 posts views Thread by Kosio | last post: by
5 posts views Thread by b83503104 | last post: by
4 posts views Thread by sofiafig | last post: by
5 posts views Thread by lim4801 | last post: by
reply views Thread by Teichintx | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.