473,403 Members | 2,183 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,403 software developers and data experts.

replacing single line of text

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

Jul 28 '06 #1
3 2474
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

Jul 29 '06 #2
sa****@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

Jul 29 '06 #3
Simon & Tim, very valuable responses. Thank you much!

Simon Forman wrote:
sa****@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
Jul 31 '06 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

11
by: Jakanapes | last post by:
Hi all, I'm looking for a way to scan a block of text and replace all the double quotes (") with single quotes ('). I'm using PHP to pull text out of a mySQL table and then feed the text into...
10
by: Xah Lee | last post by:
i have a bunch of java files that has spaced-out formatting that i want to get rid of. I want to replace two end of line characters by one end of line characters. The files in question is unix, and...
13
by: yaipa | last post by:
What would be the common sense way of finding a binary pattern in a ..bin file, say some 200 bytes, and replacing it with an updated pattern of the same length at the same offset? Also, the...
9
by: David Henderson | last post by:
Hi There... I'm struggling with a problem: I have a string (coming from a rich text editor) which contains a variety of span tags that need to be replaced with corresponding formatting tags....
11
by: lucky | last post by:
hi, i got file which contains "----------------" in a line. the line only contains this data as a saperation. using regular expression i want to i detify the line contains that data and replace...
12
by: Adam J. Schaff | last post by:
I am writing a quick program to edit a binary file that contains file paths (amongst other things). If I look at the files in notepad, they look like: ...
32
by: FireHead | last post by:
Hello C World & Fanatics I am trying replace fgets and provide a equavivalant function of BufferedInputReader::readLine. I am calling this readLine function as get_Stream. In the line 4 where...
4
by: Justin Fancy | last post by:
Hi everyone, I need to replace all instances of a double quote(") with two single quotes('') in a text file. I already have some replacements of strings going on, but I tried this one, but the...
8
by: =?Utf-8?B?UmF5IE1pdGNoZWxs?= | last post by:
I have a RichTextBox (rtfTerminal below) in which I would like to replace the last line with the last line minus its last character (i.e., do a backspace). I tried the following code, wherein I...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.