replacing single line of text

Tim Chase python.list at tim.thechases.com
Fri Jul 28 20:51:37 EDT 2006


> I want to be able to replace a single line in a large text file
> (several hundred MB). Using the cookbook's method (below) works but I
> think the replace fxn chokes on such a large chunk of text. For now, I
> simply want to replace the 1st line (CSV header) in the file but I'd
> also like to know a more general solution for any line in the file.
> There's got a be quick and dirty (and cheap) way to do this... any
> help?
> 
> Cookbook's method:
> output_file.write(input_file.read().replace(stext, rtext))

The read() method slurps the entire file into memory...bad in the 
case you describe.

What you want is to process the file as a stream:

first = True
out = file("out.txt", "w")
for line in file("foo.txt"):
	if first:
		out.write(line.replace(stext, rtext))
		first = False
	else:
		out.write(line)


If you're using an older version of Python, I don't know if 
file() returns an iterator, so you might have to do something like

	for line in file("foo.txt").xreadlines():

which I understand is the same sort of thing.  I don't know the 
history of this too well--others on the list are likely better 
versed in such peculariaties.

Or, for those *nix wonks in the crowd (self included), sed will 
do the trick nicely:

	sed '1s/stext/rtext/' foo.txt > out.txt

Just a few ideas,

-tkc






More information about the Python-list mailing list