Link to home
Start Free TrialLog in
Avatar of kimhand
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

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of RichieHindle
RichieHindle

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of kimhand
kimhand

ASKER

THANK YOU!! I've spent hours on that one.
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.