471,350 Members | 1,698 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

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

readline() - problem

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

Oct 2 '07 #1
6 1219
On Oct 2, 12:25 pm, pi...@kolodziejczyk.waw.pl wrote:
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)

--
Paul Hankin

Oct 2 '07 #2
pi***@kolodziejczyk.waw.pl writes:
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)

--
\ "The cost of a thing is the amount of what I call life which is |
`\ required to be exchanged for it, immediately or in the long |
_o__) run." -- Henry David Thoreau |
Ben Finney
Oct 2 '07 #3
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

On 02/10/2007, pi***@kolodziejczyk.waw.pl <pi***@kolodziejczyk.waw.plwrote:
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

--
http://mail.python.org/mailman/listinfo/python-list
Oct 2 '07 #4
On 2 Pa , 13:39, Ben Finney <bignose+hates-s...@benfinney.id.au>
wrote:
pi...@kolodziejczyk.waw.pl writes:
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.
Of course, it helped. Many thanks for all.

piotr

Oct 2 '07 #5
On Tue, 02 Oct 2007 12:13:21 -0000, pi***@kolodziejczyk.waw.pl wrote:
>On 2 Pa , 13:39, Ben Finney <bignose+hates-s...@benfinney.id.au>
wrote:
>pi...@kolodziejczyk.waw.pl writes:
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.

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
>
piotr
Oct 2 '07 #6
Paul Hankin wrote:
On Oct 2, 12:25 pm, pi...@kolodziejczyk.waw.pl wrote:
>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)

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

Oct 2 '07 #7

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

6 posts views Thread by Russell E. Owen | last post: by
reply views Thread by John C. Worsley | last post: by
1 post views Thread by Jian Qiu | last post: by
2 posts views Thread by Kin | last post: by
2 posts views Thread by Eddy | last post: by
6 posts views Thread by Sean Davis | last post: by

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.