RSS Forums RSS
Please support our Python advertiser: Programming Forums
Views: 1425 | Replies: 6
Reply
Join Date: Mar 2007
Location: Georgia
Posts: 13
Reputation: liz517 is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 0
liz517 liz517 is offline Offline
Newbie Poster

Help writing a censor program from a file help

  #1  
Mar 30th, 2007
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]
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Mar 2007
Posts: 1,449
Reputation: Lardmeister is an unknown quantity at this point 
Rep Power: 4
Solved Threads: 8
Lardmeister's Avatar
Lardmeister Lardmeister is offline Offline
Nearly a Posting Virtuoso

Re: writing a censor program from a file help

  #2  
Mar 30th, 2007
Why would you like to replace words like food, help, work or love with xxxx?
Reply With Quote  
Join Date: Mar 2007
Location: Georgia
Posts: 13
Reputation: liz517 is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 0
liz517 liz517 is offline Offline
Newbie Poster

Re: writing a censor program from a file help

  #3  
Mar 30th, 2007
because that is the parameters of the assignment.
Reply With Quote  
Join Date: Mar 2007
Location: Georgia
Posts: 13
Reputation: liz517 is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 0
liz517 liz517 is offline Offline
Newbie Poster

Re: writing a censor program from a file help

  #4  
Mar 30th, 2007
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.
Reply With Quote  
Join Date: Mar 2007
Posts: 1,449
Reputation: Lardmeister is an unknown quantity at this point 
Rep Power: 4
Solved Threads: 8
Lardmeister's Avatar
Lardmeister Lardmeister is offline Offline
Nearly a Posting Virtuoso

Re: writing a censor program from a file help

  #5  
Mar 30th, 2007
I think it goes like that:
  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!
Reply With Quote  
Join Date: Mar 2007
Location: Georgia
Posts: 13
Reputation: liz517 is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 0
liz517 liz517 is offline Offline
Newbie Poster

Troubleshooting Re: writing a censor program from a file help

  #6  
Mar 30th, 2007
Originally Posted by Lardmeister View Post
I think it goes like that:
  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!
  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?
Reply With Quote  
Join Date: Oct 2004
Posts: 2,536
Reputation: vegaseat will become famous soon enough vegaseat will become famous soon enough 
Rep Power: 11
Solved Threads: 178
Moderator
vegaseat's Avatar
vegaseat vegaseat is offline Offline
DaniWeb's Hypocrite

Re: writing a censor program from a file help

  #7  
Mar 31st, 2007
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.
May 'the Google' be with you!
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes
Forums | Blogs | Tutorials | Code Snippets | Whitepapers | RSS Feeds | Advertising
All times are GMT -4. The time now is 10:35 am.
Newsletter Archive - Sitemap - Privacy Statement - Contact Us
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC