473,225 Members | 1,282 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,225 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 1274
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Russell E. Owen | last post by:
At one time, mixing for x in file and readline was dangerous. For example: for line in file: # read some lines from a file, then break nextline = readline() # bad would not do what a naive...
0
by: John C. Worsley | last post by:
I've got an extremely inscrutable problem here using Perl's Term::ReadLine::Gnu module. I'm using Perl 5.8.0, readline 4.3 and Term::ReadLine::Gnu 1.14. The problem is specific to catching INT...
1
by: Jian Qiu | last post by:
Hi, I tried to install python2.4.2. Unfortunately building 'readline' extension failed. Here is what I got: (It is a bit long. If you are impatient, please look at the end where it reports the...
1
by: Kevin | last post by:
In a newsgroup thread from Jan 8, 2003 between Barry Holsinger and the VBDotNet Team, please review this excerpt: "You understood my problem completely. Your sample code provides a really...
2
by: Kin | last post by:
I am trying to read the output of an external application using redirected stdout and StreamReader::ReadLine(). The problem is that ReadLine() blocks and I am either reading nothing or just part...
2
by: Eddy | last post by:
I have a big problem with streamreader ReadLine()! I read from a long text files about 13k lines, than I encounter a problem: ReadLine() is not anymore able to go on! I have a string whose name is...
6
by: Sean Davis | last post by:
I have a large file that I would like to transform and then feed to a function (psycopg2 copy_from) that expects a file-like object (needs read and readline methods). I have a class like so: ...
0
by: Akira Kitada | last post by:
Hi list, I was trying to build Python 2.6 on FreeBSD 4.11 and found it failed to build some of the modules. """ Failed to find the necessary bits to build these modules: _bsddb ...
0
by: M.-A. Lemburg | last post by:
On 2008-10-25 08:39, Akira Kitada wrote: Please post a bug report on python.org about these failures. The multiprocessing module is still fairly new and obviously needs more fine tuning for...
0
by: Akira Kitada | last post by:
Hi Marc-Andre, Thanks for the suggestion. I opened a ticket for this issue: http://bugs.python.org/issue4204 Now I understand the state of the multiprocessing module, but it's too bad to see...
1
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...
0
by: veera ravala | last post by:
ServiceNow is a powerful cloud-based platform that offers a wide range of services to help organizations manage their workflows, operations, and IT services more efficiently. At its core, ServiceNow...
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 3 Jan 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). For other local times, please check World Time Buddy In...
0
by: jianzs | last post by:
Introduction Cloud-native applications are conventionally identified as those designed and nurtured on cloud infrastructure. Such applications, rooted in cloud technologies, skillfully benefit from...
0
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
2
by: jimatqsi | last post by:
The boss wants the word "CONFIDENTIAL" overlaying certain reports. He wants it large, slanted across the page, on every page, very light gray, outlined letters, not block letters. I thought Word Art...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....

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.