[Tutor] Text processing basic Q

antonmuhin at rambler.ru antonmuhin at rambler.ru" <antonmuhin@rambler.ru
Mon Apr 14 12:14:02 2003


Hello jamesgross,

Monday, April 14, 2003, 7:48:44 PM, you wrote:

jsb> HI-

jsb> I have a log file(from irc chat #plone) that I would
jsb> like to print. The log file has lines that start with * that I would like
jsb> to delete from
jsb> the file.

jsb> Yesterday <Erwin> from #python helped answer how to select a line that
jsb> starts with a * by line.startswith('*'), so now  what would the code be
jsb> for a script to

jsb> 1. load the file
jsb> 2. start at the beginning of the file
jsb> 3. if line start with * then delete
jsb> 4. move to the next line in the file
jsb> 5. save the file

jsb> Thanks in advance, James

WARNING: not tested

file_name = ...

input_file = file(file_name)
filtered_lines = [l for l in input_file if not l.startswith('*')]
input_file.close()

output_file = file(file_name, 'w')
for l in filtered_lines:
     output_file.write(l)
output_file.close()

If you want les memory-consuming version, create a filtered file and
cpy it later into original file.

HTH

-- 
Best regards,
 anton                            mailto:antonmuhin@rambler.ru