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

extracting numbers from a file, excluding fixed words

P: n/a
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
Share this Question
Share on Google+
5 Replies


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

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

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

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

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