I need to read in a text file, replace selected words and write the changed text back out to a file. Is there an easy way with Python?
sneekula
969
Nearly a Posting Maven
Recommended Answers
Jump to PostI need to read in a text file, replace selected words and write the changed text back out to a file. Is there an easy way with Python?
there are several ways.
1) reading with for loopo = open("output","a") #open for append for line in open("file"): …
Jump to PostHere is an example, where the target and replacement words are in a dictionary. The Python module re is used to do the gruntwork ...
# read a text file, replace multiple words specified in a dictionary # write the modified text back to a file import …
All 13 Replies
ghostdog74
57
Junior Poster
bhuenefeld88
commented:
It helped me out quickly when needed
+0
sneekula
969
Nearly a Posting Maven
vegaseat
1,735
DaniWeb's Hypocrite
Team Colleague
sneekula
969
Nearly a Posting Maven
Shraddha Kiran
0
Newbie Poster
cheezystix
0
Newbie Poster
romes87
0
Newbie Poster
romes87
0
Newbie Poster
TrustyTony
888
pyMod
Team Colleague
Featured Poster
romes87
0
Newbie Poster
romes87
0
Newbie Poster
romes87
0
Newbie Poster
halamas
0
Newbie Poster
Be a part of the DaniWeb community
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.