[2.5.1] Read each line from txt file, replace, and save?

Gilles nospam at nospam.com
Sun Sep 2 06:19:02 EDT 2012


Hello

This is a newbie question.

I need to read a text file into a variable, loop through each line and
use a regex to substitute some items within the line, and save the
whole variable into a new text file.

This triggers an error when I save the modified variable that contains
all the lines:
==================
import re,sys

f = open("C:\\input.txt", "r")
textlines = f.readlines()
f.close()

for line in textlines:
	#edit each line
	line = "just a test"
	
#rewrite data to new file
log = open('output.sub','w')
#ERROR: argument 1 must be string or read-only character buffer, not
list
log.write(textlines)
log.close()
==================

Should I use another way to read the file, edit each line, and save
the data into a new file?

Thank you.



More information about the Python-list mailing list