readline() - problem

P

piotr

Hi!
I'm a new user of python, and have problem.
I have a plain ascii file:
1aaaaaaaaaaaa1......1
1cccccccccccc2......1
1xxxxxxxxxxxx1......1
I want to create a new file which contains only lines with '1' on 15th
position.
I've tried this:

import string
f=open('/test/test.asc','r')
o=open('/test/out.asc','w')
for line in f:
s= f.readline()
if s[15]=='1' :
o.write(s)
o.close()
f.close()

Why it doesn't work ('s' contains ' ' )?

piotr
 
P

Paul Hankin

Hi!
I'm a new user of python, and have problem.
I have a plain ascii file:
1aaaaaaaaaaaa1......1
1cccccccccccc2......1
1xxxxxxxxxxxx1......1
I want to create a new file which contains only lines with '1' on 15th
position.
I've tried this:

import string
f=open('/test/test.asc','r')
o=open('/test/out.asc','w')
for line in f:
s= f.readline()
if s[15]=='1' :
o.write(s)
o.close()
f.close()

Why it doesn't work ('s' contains ' ' )?

You're iterating over the lines in f already, so no need to call
readline.

for line in f:
if line[15] == '1':
o.write(line)
 
B

Ben Finney

import string

Why import 'string' if you're not using it?
f=open('/test/test.asc','r')
o=open('/test/out.asc','w')
for line in f:
s= f.readline()

Your line object is already bound to the 'line' name in each
iteration. You need to use that, not attempt to read yet another line
each time.

That is, instead of::

for line in f:
foo = f.readline()
do_interesting_thing(foo)

you should do this::

for line in f:
do_interesting_thing(line)
 
W

Wesley Brooks

Given a file:

#### t.txt ####
1 2
1 1
3 1
### end of file ###

file = open('t.txt', 'r')
for i, line in enumerate(file):
print "Line:", i, "Text:", line

would give the result:

Line: 0 Text: 1 2
Line: 1 Text: 1 1
Line: 2 Text: 3 1

To check the third character in each line:

for line in file:
if (line != '') and (len(line) > 2):
if line[2] == '1':
print "Got line:", line

Would give:

Got line: 1 1
Got line: 3 1

Cheers,

Wesley Brooks
 
P

piotr

Why import 'string' if you're not using it?


Your line object is already bound to the 'line' name in each
iteration. You need to use that, not attempt to read yet another line
each time.

Of course, it helped. Many thanks for all.

piotr
 
W

Wayne Brehaut

Of course, it helped. Many thanks for all.

But be sure you note Wesley's point in teh following post:

If you want the 15th character your subscript must be 14, since
there's a 0th element?

wwwayne
 
?

=?ISO-8859-1?Q?Ricardo_Ar=E1oz?=

Paul said:
Hi!
I'm a new user of python, and have problem.
I have a plain ascii file:
1aaaaaaaaaaaa1......1
1cccccccccccc2......1
1xxxxxxxxxxxx1......1
I want to create a new file which contains only lines with '1' on 15th
position.
I've tried this:

import string
f=open('/test/test.asc','r')
o=open('/test/out.asc','w')
for line in f:
s= f.readline()
if s[15]=='1' :
o.write(s)
o.close()
f.close()

Why it doesn't work ('s' contains ' ' )?

You're iterating over the lines in f already, so no need to call
readline.

for line in f:
if line[15] == '1':
o.write(line)

Be aware also that the 15th position in your line would be line[14].
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,431
Messages
2,571,677
Members
48,796
Latest member
Greg L.

Latest Threads

Top