| | |
writing a censor program from a file help
Please support our Python advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
Join Date: Mar 2007
Posts: 13
Reputation:
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
Posts: 13
Reputation:
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!
![]() |
Similar Threads
- Writing to binary file, erases previous data? (C++)
- Help with Java program writing (Java)
- Checking if a file is in Writing/Saving mode (VB.NET)
Other Threads in the Python Forum
- Previous Thread: tkinter find
- Next Thread: censor problems again
| Thread Tools | Search this Thread |
accessdenied apache application argv array beginner book builtin change chmod converter countpasswordentry curved dan08 dictionary dynamic edit enter examples file filename float format function gui homework import inches input java keyboard lapse library line lines linux list lists loop microphone mouse movingimageswithpygame mysql mysqlquery newb number numbers numeric output parameters parsing path phonebook plugin port prime programming projects py2exe pygame pyopengl pysimplewizard python random recursion redirect remote reverse scrolledtext session simple smtp software sprite statictext string strings syntax table tennis terminal text textarea thread threading time tkinter tlapse trick tuple tutorial ubuntu unicode unit urllib urllib2 variable windows wordgame wxpython






