[Tutor] Parsing text file with Python
Jay Mutter III
jmutter at uakron.edu
Fri Mar 23 22:32:53 CET 2007
Script i have to date is below and
Thanks to your help i can see some daylight but I still have a few
questions
1.) Are there better ways to write this?
2.) As it writes out the one group to the new file for companies it
is as if it leaves blank lines behind for if I don't have the elif len
(line) . 1 the
inventor's file has blank lines in it.
3.) I reopened the inventor's file to get a count of lines but is
there a better way to do this?
Thanks
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.readlines()
count = len(text)
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.endswith(')\n'):
companies.write(line)
elif line.endswith(') \n'):
companies.write(line)
elif len(line) > 1:
patentdata.write(line)
in_file.close()
companies.close()
patentdata.close()
in_filename2 = raw_input('What was the name of the inventor\'s
file ? ')
in_file2 = open(in_filename2, 'rU')
text2 = in_file2.readlines()
count = len(text2)
print "There are - well until we clean up more - approximately ",
count, 'inventor\s in this file'
More information about the Tutor
mailing list