kimhand
asked on
The replace function in Python
I need my app to read a text, and replace the word "bomb" with *** I'm not having any luck can anyone help?
#!/usr/bin/python
n = open("mosque.txt").read()
n=n.replace("\n"," ") # get rid of newlines completely
n=n.replace(".",".#SENT#")
n=n.replace("?","?#SENT#")
n=n.replace("!","!#SENT#")
text = n.split("#SENT")
for i in range (0, len(n)):
print n[i]
for i in range (0,len(n)):
s = n[i].replace("bomb"," *** BOMB ***")
print i, s
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Here's a tip to help you track down problems like this in future: use meaningful variable names that reflect the type of the information. Rather than "n", how about "original_text", and rather than "text", how about "fragment_list" or similar. Then when you saw yourself saying "original_text[i]", an alarm bell would ring in your head because it's not a list.
ASKER