RSS Forums RSS
Please support our Python advertiser: Programming Forums
Views: 797 | Replies: 2
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

Troubleshooting censor problems again

  #1  
Mar 31st, 2007
This is what I have. I know I am missing something can you help
[code = python]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()[/code]
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Oct 2004
Posts: 2,537
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 online now Online
DaniWeb's Hypocrite

Re: censor problems again

  #2  
Mar 31st, 2007
In your code tag [code = python] don't use any spaces! It's [code=python]
May 'the Google' be with you!
Reply With Quote  
Join Date: Oct 2006
Posts: 1,669
Reputation: sneekula is on a distinguished road 
Rep Power: 6
Solved Threads: 47
sneekula's Avatar
sneekula sneekula is offline Offline
Posting Virtuoso

Re: censor problems again

  #3  
Apr 1st, 2007
I didn't have a file, so I used just a text string for testing, but here is one way you can solve this:
  1. text = "I love to work, but not paying my taxes!"
  2.  
  3. # text to word list
  4. words = text.split()
  5. print words # testing the word list
  6.  
  7. # now comes the processing part
  8. new_words = []
  9. for word in words:
  10. # remove certain punctuations
  11. for c in '?!,.;:':
  12. if c in word:
  13. word = word.replace(c, '')
  14. # it's a 4 letter word, replace with 'xxxx'
  15. if len(word) == 4:
  16. word = 'xxxx'
  17. new_words.append(word)
  18.  
  19. # list back to text
  20. print ' '.join(new_words)
No one died when Clinton lied.
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 12:08 pm.
Newsletter Archive - Sitemap - Privacy Statement - Contact Us
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC