944,218 Members | Top Members by Rank

Ad:
  • Python Discussion Thread
  • Unsolved
  • Views: 2535
  • Python RSS
Mar 30th, 2007
0

writing a censor program from a file help

Expand Post »
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]
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
liz517 is offline Offline
13 posts
since Mar 2007
Mar 30th, 2007
0

Re: writing a censor program from a file help

Why would you like to replace words like food, help, work or love with xxxx?
Reputation Points: 407
Solved Threads: 36
Posting Virtuoso
Lardmeister is offline Offline
1,701 posts
since Mar 2007
Mar 30th, 2007
0

Re: writing a censor program from a file help

because that is the parameters of the assignment.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
liz517 is offline Offline
13 posts
since Mar 2007
Mar 30th, 2007
0

Re: writing a censor program from a file help

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.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
liz517 is offline Offline
13 posts
since Mar 2007
Mar 30th, 2007
0

Re: writing a censor program from a file help

I think it goes like that:
python Syntax (Toggle Plain Text)
  1. text = "I love to work and pay my taxes!"
  2.  
  3. # text to list
  4. words = text.split()
  5. print words # ['I', 'love', 'to', 'work', 'and', 'pay', 'my', 'taxes!']
  6.  
  7. # list to text
  8. print ' '.join(words) # I love to work and pay my taxes!
Reputation Points: 407
Solved Threads: 36
Posting Virtuoso
Lardmeister is offline Offline
1,701 posts
since Mar 2007
Mar 30th, 2007
0

Re: writing a censor program from a file help

I think it goes like that:
python Syntax (Toggle Plain Text)
  1. text = "I love to work and pay my taxes!"
  2.  
  3. # text to list
  4. words = text.split()
  5. print words # ['I', 'love', 'to', 'work', 'and', 'pay', 'my', 'taxes!']
  6.  
  7. # list to text
  8. print ' '.join(words) # I love to work and pay my taxes!
python Syntax (Toggle Plain Text)
  1. # wordcensor.py
  2. # Program replaces 4 letter words in a text file.
  3.  
  4. import string
  5.  
  6. def main():
  7. print "This program replaces 4 lettter words in a file with xxxx"
  8.  
  9. # get the sequence of words from the file
  10. fname = raw_input("File to analyze: ")
  11. text = open(fname,'r').read()
  12. text = string.lower(text)
  13. for ch in '!"#$%&\'()*+,-./:;<=>?@[\\]^_`{|}~':
  14. text = string.replace(text, ch, ' ')
  15. words = string.split(text)
  16. wordCount = len(words)
  17. ch = string.split(words)
  18. charCount = len(ch)
  19. if charCount == 4:
  20. oldword = string.join(ch)
  21. newtext = string.join(text)
  22. o = open(fname,a)
  23. for line in open(fname):
  24. line = line.replace(oldword,newword)
  25. o.write(line + "\n")
  26. o.close()
  27.  
  28.  
  29.  
  30. text.close()
  31.  
  32. main()
This is what I have now what am I doing wrong?
Reputation Points: 10
Solved Threads: 0
Newbie Poster
liz517 is offline Offline
13 posts
since Mar 2007
Mar 31st, 2007
0

Re: writing a censor program from a file help

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!
Last edited by vegaseat; Mar 31st, 2007 at 12:57 pm.
Moderator
Reputation Points: 1333
Solved Threads: 1404
DaniWeb's Hypocrite
vegaseat is offline Offline
5,792 posts
since Oct 2004

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Python Forum Timeline: tkinter find
Next Thread in Python Forum Timeline: censor problems again





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC