I am to write a censor program for all 4 letter words from an imported file, only I'm stuck. can you help?

# 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()

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

because that is the parameters of the assignment.

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.

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!

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?

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!

Did you ever figure this out??

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.