[Tutor] Another string question

Jay Mutter III jmutter at uakron.edu
Fri Mar 23 11:34:49 CET 2007


On Mar 23, 2007, at 5:30 AM, Andre Engels wrote:

> 2007/3/22, Jay Mutter III <jmutter at uakron.edu>:
> I wanted the following to check each line and if it ends in a right
> parentheses then write the entire line to one file and if not then
> write the line to anther.
> It wrote all of the ) to one file and the rest of the line (ie minus
> the ) to the other file.
>
> The line:
>      print "There are ", count, 'lines to process in this file'
> should give you a hint - don't you think this number was rather high?
>
> The problem is that if you do "for line in text" with text being a  
> string, it will not loop over the _lines_  in the string, but over  
> the _characters_ in the string.
>
> The easiest solution would be to replace
>      text = in_file.read()
> by
>      text = in_file.readlines()
>

Thanks for the response
Actually the number of lines this returns is the same number of lines  
given when i put it in a text editor (TextWrangler).
Luke had mentioned the same thing earlier but when I do change read  
to readlines  i get the following


Traceback (most recent call last):
   File "extract_companies.py", line 17, in ?
     count = len(text.splitlines())
AttributeError: 'list' object has no attribute 'splitlines'



> in_filename = raw_input('What is the COMPLETE name of the file you
> would like to process?    ')
> in_file = open(in_filename, 'rU')
> text = in_file.read()
> count = len(text.splitlines())
> print "There are ", count, 'lines to process in this file'
> out_filename1 = raw_input('What is the COMPLETE name of the file in
> which you would like to save Companies?    ')
> companies = open(out_filename1, 'aU')
> out_filename2 = raw_input('What is the COMPLETE name of the file in
> which you would like to save Inventors?    ')
> patentdata = open(out_filename2, 'aU')
> for line in text:
>      if line[-1] in ')':
>          companies.write(line)
>      else:
>          patentdata.write(line)
> in_file.close()
> companies.close()
> patentdata.close()
>
> Thanks
>
> jay
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> http://mail.python.org/mailman/listinfo/tutor
>
>
>
> -- 
> Andre Engels, andreengels at gmail.com
> ICQ: 6260644  --  Skype: a_engels

-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.python.org/pipermail/tutor/attachments/20070323/fd5f1670/attachment.htm 


More information about the Tutor mailing list