readline() - problem

Ricardo Aráoz ricaraoz at gmail.com
Tue Oct 2 19:11:46 EDT 2007


Paul Hankin wrote:
> On Oct 2, 12:25 pm, pi... at 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].




More information about the Python-list mailing list