Python editing .txt file

187braintrust at berkeley.edu 187braintrust at berkeley.edu
Wed Jun 16 15:29:21 EDT 2010


MRAB <python <at> mrabarnett.plus.com> writes:
> input_file = open(input_path)
> output_file = open(output_path, "w")
> for line in input_file:
>      if line.startswith("factor("):
>          open_paren = line.find("(")
>          close_paren = line.find(")")
>          variable = line[open_paren + 1 : close_paren]
>          output_file.write("*** Factors for %s ***\n" % variable)
>          prefix = line[ : close_paren + 1]
>          while line.startswith(prefix):
>              line = input_file.readline()
>      output_file.write(line)
> input_file.close()
> output_file.close() 

This code is very helpful.  Thank you.  I have been working with it, but 
encounter an error that I thought I should mention (line = input_file.readline() 
ValueError: Mixing iteration and read methods would lose data).  




More information about the Python-list mailing list