writing a censor program from a file help

Please support our Python advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Mar 2007
Posts: 13
Reputation: liz517 is an unknown quantity at this point 
Solved Threads: 0
liz517 liz517 is offline Offline
Newbie Poster

writing a censor program from a file help

 
0
  #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]
Reply With Quote Quick reply to this message  
Join Date: Mar 2007
Posts: 1,540
Reputation: Lardmeister is an unknown quantity at this point 
Solved Threads: 22
Lardmeister's Avatar
Lardmeister Lardmeister is offline Offline
Posting Virtuoso

Re: writing a censor program from a file help

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

Re: writing a censor program from a file help

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

Re: writing a censor program from a file help

 
0
  #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 Quick reply to this message  
Join Date: Mar 2007
Posts: 1,540
Reputation: Lardmeister is an unknown quantity at this point 
Solved Threads: 22
Lardmeister's Avatar
Lardmeister Lardmeister is offline Offline
Posting Virtuoso

Re: writing a censor program from a file help

 
0
  #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 Quick reply to this message  
Join Date: Mar 2007
Posts: 13
Reputation: liz517 is an unknown quantity at this point 
Solved Threads: 0
liz517 liz517 is offline Offline
Newbie Poster

Re: writing a censor program from a file help

 
0
  #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 Quick reply to this message  
Join Date: Oct 2004
Posts: 4,047
Reputation: vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice vegaseat is just really nice 
Solved Threads: 935
Moderator
vegaseat's Avatar
vegaseat vegaseat is offline Offline
DaniWeb's Hypocrite

Re: writing a censor program from a file help

 
0
  #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 Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC