954,549 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

writing a censor program from a file help

I am to write a censor program for all 4 letter words from an imported file, only I'm stuck. can you help?
[code = python]
# wordcensor.py
# Program replaces 4 letter words in a text file.

import string

def main():
print "This program replaces 4 lettter words in a file with xxxx"

# get the sequence of words from the file
fname = raw_input("File to analyze: ")
text = open(fname,'r').read()
text = string.lower(text)
for ch in '!"#$%&\'()*+,-./:;<=>?@[\\]^_`{|}~':
text = string.replace(text, ch, ' ')
words = string.split(text)
wordCount = len(words)
ch = string.split(words)
charCount = len(ch)
if charCount == 4:



main()

[/code]

liz517
Newbie Poster
13 posts since Mar 2007
Reputation Points: 10
Solved Threads: 0
 

Why would you like to replace words like food, help, work or love with xxxx?

Lardmeister
Posting Virtuoso
1,749 posts since Mar 2007
Reputation Points: 407
Solved Threads: 44
 

because that is the parameters of the assignment.

liz517
Newbie Poster
13 posts since Mar 2007
Reputation Points: 10
Solved Threads: 0
 

what I need to know is how to "unsplit" the word. I know how to use replace(old,new) I just don't know how to put the characters back into the word form.

liz517
Newbie Poster
13 posts since Mar 2007
Reputation Points: 10
Solved Threads: 0
 

I think it goes like that:

text = "I love to work and pay my taxes!"
 
# text to list
words = text.split()
print words  # ['I', 'love', 'to', 'work', 'and', 'pay', 'my', 'taxes!']
 
# list to text
print ' '.join(words)  # I love to work and pay my taxes!
Lardmeister
Posting Virtuoso
1,749 posts since Mar 2007
Reputation Points: 407
Solved Threads: 44
 

I think it goes like that:

text = "I love to work and pay my taxes!"
 
# text to list
words = text.split()
print words  # ['I', 'love', 'to', 'work', 'and', 'pay', 'my', 'taxes!']
 
# list to text
print ' '.join(words)  # I love to work and pay my taxes!
# wordcensor.py
#     Program replaces 4 letter words in a text file.

import string

def main():
    print "This program replaces 4 lettter words in a file with xxxx"
    
    # get the sequence of words from the file
    fname = raw_input("File to analyze: ")
    text = open(fname,'r').read()
    text = string.lower(text)
    for ch in '!"#$%&\'()*+,-./:;<=>?@[\\]^_`{|}~':
        text = string.replace(text, ch, ' ')
    words = string.split(text)
    wordCount = len(words)
    ch = string.split(words)
    charCount = len(ch)
    if charCount == 4:
             oldword = string.join(ch)
             newtext = string.join(text)
             o = open(fname,a)
             for line in open(fname):
                    line = line.replace(oldword,newword)
                    o.write(line + "\n") 
             o.close()

        
                
    text.close()     
   
main()


This is what I have now what am I doing wrong?

liz517
Newbie Poster
13 posts since Mar 2007
Reputation Points: 10
Solved Threads: 0
 

I think it would be easier, if you ...

1) read the file in as a string
2) convert to a list of words
3) process each word in the list
4) create a new list of the process results
5) join the new list back to a string
6) save this modified string to a file

You have pretty much accomplished items 1, 2 and 3

If you want to retain the punctuation marks in the modified text, you have to put your thinking cap on! This will be a challenge!

vegaseat
DaniWeb's Hypocrite
Moderator
5,989 posts since Oct 2004
Reputation Points: 1,345
Solved Threads: 1,417
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You