473,386 Members | 2,050 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,386 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 1276
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...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

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.