Please support our Python advertiser: Programming Forums
Views: 1425 | Replies: 6
![]() |
•
•
Join Date: Mar 2007
Location: Georgia
Posts: 13
Reputation:
Rep Power: 2
Solved Threads: 0
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]
[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]
I think it goes like that:
python Syntax (Toggle Plain Text)
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!
•
•
Join Date: Mar 2007
Location: Georgia
Posts: 13
Reputation:
Rep Power: 2
Solved Threads: 0
•
•
•
•
I think it goes like that:
python Syntax (Toggle Plain Text)
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!
python Syntax (Toggle Plain Text)
# 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()
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!
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!
![]() |
•
•
•
•
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)






Linear Mode