[Tutor] Another string question

Andre Engels andreengels at gmail.com
Fri Mar 23 10:30:51 CET 2007


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()

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/f900c484/attachment.htm 


More information about the Tutor mailing list