2007/3/23, Jay Mutter III <[EMAIL PROTECTED]>:


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

2007/3/22, Jay Mutter III < [EMAIL PROTECTED]>:
>
> 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'



Ah, yes, there you DO split in lines, but later you don't. You'll have to do
the same thing twice, that is either:

text = in_file.readlines()
count = len(text) # (instead of count = len(text.splitlines())

OR

text = in_file.read()
for line in text.splitlines(): # (instead of for line in text:)

in_filename = raw_input('What is the COMPLE
>
 TE 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@python.org
> http://mail.python.org/mailman/listinfo/tutor
>



--
Andre Engels, [EMAIL PROTECTED]
ICQ: 6260644  --  Skype: a_engels





--
Andre Engels, [EMAIL PROTECTED]
ICQ: 6260644  --  Skype: a_engels
_______________________________________________
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor

Reply via email to