replacing single line of text

Simon Forman rogue_pedro at yahoo.com
Fri Jul 28 21:02:21 EDT 2006


saad82 at gmail.com wrote:
> 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))
>
> Thanks,
> Pythonner

The read() method of a file will "read all data until EOF is reached"
if you don't pass it a size argument.  If your file is "several hundred
MB" and your RAM is not, you may have some trouble with this.

I don't know how well the replace() method works on tenths-of-a-GB
strings.

The file object supports line-by-line reads through both the readline()
and readlines() methods, but be aware that the readlines() method will
also try to read ALL the data into memory at once unless you pass it a
size argument.

You can also iterate through the lines in an open file object like so:

for line in input_file:
    # Do something with the line here.

so, if you know that you want to replace a whole actual line, you could
do this like so:

for line in input_file:
    if line == stext:
        output_file.write(rtext)
    else:
        output_file.write(line)


Check out the docs on the file object for more info:
http://docs.python.org/lib/bltin-file-objects.html


HTH,
~Simon




More information about the Python-list mailing list