By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
432,403 Members | 855 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 432,403 IT Pros & Developers. It's quick & easy.

Search & Replace

P: n/a
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()

Oct 26 '06 #1
Share this Question
Share on Google+
6 Replies


P: n/a
In <11**********************@f16g2000cwb.googlegroups .com>, DataSmash
wrote:
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
Oct 26 '06 #2

P: n/a
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
Oct 26 '06 #3

P: n/a
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
Oct 26 '06 #4

P: n/a

DataSmash wrote:
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....17f004e?hl=en&
)

- Pad.

Oct 27 '06 #5

P: n/a
DataSmash wrote:
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%20beta. This is how it works:
>>replacement_definitions = '''
Left_RefAddr=FromLeft
Left_NonRefAddr=ToLeft
Right_RefAddr=FromRight
Right_NonRefAddr=ToRight
'''
>>import SE
Replacements = SE.SE (replacement_definitions)
Replacements ('text.txt', 'new_text.txt')
That's all! Or in place:
>>ALLOW_IN_PLACE = 3
Replacements.set (file_handling_flag = ALLOW_IN_PLACE)
Replacements ('text.txt')
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:
>>print Replacements (replacement_definitions) # Use definitions as
test data

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

Checks out. All substitutions are made.
Regards

Frederic
Oct 27 '06 #6

P: n/a
Really appreciate all the all the different answers and learning tips!

Oct 27 '06 #7

This discussion thread is closed

Replies have been disabled for this discussion.