Search & Replace

D

DataSmash

Hello,
I need to search and replace 4 words in a text file.
Below is my attempt at it, but this code appends
a copy of the text file within itself 4 times.
Can someone help me out.
Thanks!

# Search & Replace
file = open("text.txt", "r")
text = file.read()
file.close()

file = open("text.txt", "w")
file.write(text.replace("Left_RefAddr", "FromLeft"))
file.write(text.replace("Left_NonRefAddr", "ToLeft"))
file.write(text.replace("Right_RefAddr", "FromRight"))
file.write(text.replace("Right_NonRefAddr", "ToRight"))
file.close()
 
M

Marc 'BlackJack' Rintsch

DataSmash said:
I need to search and replace 4 words in a text file.
Below is my attempt at it, but this code appends
a copy of the text file within itself 4 times.

Because you `write()` the whole text four times to the file. Make the 4
replacements first and rebind `text` to the string with the replacements
each time, and *then* write the result *once* to the file.
# Search & Replace
file = open("text.txt", "r")
text = file.read()
file.close()

file = open("text.txt", "w")

text = text.replace("Left_RefAddr", "FromLeft")
text = text.replace("Left_NonRefAddr", "ToLeft")
# ...
file.write(text)
file.close()

Ciao,
Marc 'BlackJack' Rintsch
 
T

Tim Chase

Below is my attempt at it, but this code appends
a copy of the text file within itself 4 times.
Can someone help me out. [snip]
file = open("text.txt", "w")
file.write(text.replace("Left_RefAddr", "FromLeft"))
file.write(text.replace("Left_NonRefAddr", "ToLeft"))
file.write(text.replace("Right_RefAddr", "FromRight"))
file.write(text.replace("Right_NonRefAddr", "ToRight"))
file.close()


Well, as you can see, you're writing (write()) the text 4 times.

Looks like you want something like

file.write(text.replace("Left_RefAddr",
"FromLeft").replace("Left_NonRefAddr",
"ToLeft").replace("Right_RefAddr",
"FromRight").replace("Right_NonRefAddr", "ToRight"))


which is about the equiv. of

text = text.replace(...1...)
text = text.replace(...2...)
text = text.replace(...3...)
text = text.replace(...4...)
file.write(text)

I would also be remiss if I didn't mention that it's generally
considered bad form to use the variable-name "file", as it
shadows the builtin "file".

There are additional ways if replacements cause problems that
then themselves get replaced, and this is an undesired behavior.
However, it looks like your example doesn't have this problem,
so the matter is moot.

-tkc
 
B

Bruno Desthuilliers

DataSmash a écrit :
Hello,
I need to search and replace 4 words in a text file.
Below is my attempt at it, but this code appends
a copy of the text file within itself 4 times.
Can someone help me out.
Thanks!

# Search & Replace
file = open("text.txt", "r")
NB : avoid using 'file' as an identifier - it shadows the builtin 'file'
type.
text = file.read()
file.close()

file = open("text.txt", "w")
file.write(text.replace("Left_RefAddr", "FromLeft"))
file.write(text.replace("Left_NonRefAddr", "ToLeft"))
file.write(text.replace("Right_RefAddr", "FromRight"))
file.write(text.replace("Right_NonRefAddr", "ToRight"))
file.close()

See Mark and Tim's answers for your bug. Another (potential) problem
with your code is that it may not work too well for big files. It's ok
if you know that the files content will always be small enough to not
eat all memory. Else, taking a "line by line" approach is the canonical
solution :

def simplesed(src, dest, *replacements):
for line in src:
for target, repl in replacements:
line = line.replace(target, repl)
dest.write(line)

replacements = [
("Left_RefAddr", "FromLeft"),
("Left_NonRefAddr", "ToLeft"),
("Right_RefAddr", "FromRight"),
("Right_NonRefAddr", "ToRight"),
]
src = open("hugetext.txt", "r")
dest = open("some-temp-name.txt", "w")
simplesed(src, dest, *replacements)
src.close()
dest.close()
os.rename("some-temp-name.txt", "hugetext.txt")

HTH
 
P

Paddy

DataSmash said:
Hello,
I need to search and replace 4 words in a text file.
Below is my attempt at it, but this code appends
a copy of the text file within itself 4 times.
Can someone help me out.
Thanks!

# Search & Replace
file = open("text.txt", "r")
text = file.read()
file.close()

file = open("text.txt", "w")
file.write(text.replace("Left_RefAddr", "FromLeft"))
file.write(text.replace("Left_NonRefAddr", "ToLeft"))
file.write(text.replace("Right_RefAddr", "FromRight"))
file.write(text.replace("Right_NonRefAddr", "ToRight"))
file.close()

Check out the Pythons standard fileinput module. It also has options
for in-place editing.

(
http://groups.google.com/group/comp.lang.python/msg/2d88c0f5e17f004e?hl=en&
)

- Pad.
 
F

Frederic Rentsch

DataSmash said:
Hello,
I need to search and replace 4 words in a text file.
Below is my attempt at it, but this code appends
a copy of the text file within itself 4 times.
Can someone help me out.
Thanks!

# Search & Replace
file = open("text.txt", "r")
text = file.read()
file.close()

file = open("text.txt", "w")
file.write(text.replace("Left_RefAddr", "FromLeft"))
file.write(text.replace("Left_NonRefAddr", "ToLeft"))
file.write(text.replace("Right_RefAddr", "FromRight"))
file.write(text.replace("Right_NonRefAddr", "ToRight"))
file.close()

Here's a perfect problem for a stream editor, like
http://cheeseshop.python.org/pypi/SE/2.2 beta. This is how it works:
Left_RefAddr=FromLeft
Left_NonRefAddr=ToLeft
Right_RefAddr=FromRight
Right_NonRefAddr=ToRight
'''
That's all! Or in place:

This should solve your task.

An SE object takes strings too, which is required for line-by-line
processing and is very useful for development or verification:
test data

FromLeft=FromLeft
ToLeft=ToLeft
FromRight=FromRight
ToRight=ToRight

Checks out. All substitutions are made.


Regards

Frederic
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,230
Members
46,817
Latest member
DicWeils

Latest Threads

Top