
November 8th, 2012, 04:37 PM
|
|
Registered User
|
|
Join Date: Nov 2012
Posts: 1
Time spent in forums: 31 m 30 sec
Reputation Power: 0
|
|
|
Opening text file, converting one four letter word to another
I am trying to open a saved text file that has a short sentence in it and then convert all four letter words in the sentence to "XXXX". Then save it back to the file. So far I am struggling with this. I pasted what I have done so far, but seem to be stuck now. I'm not sure what is wrong with what I have or if I am just missing something. Also I am new to the forum so if someone can tell me how to post my program properly, I will fix it. Any help would be greatly appreciated!
def censor():
filename = input("Enter name of file: ")
file = open(filename, 'r+')
i = 0
alist = []
for element in file:
words = element.split()
for i in range(len(words)):
if len(words[i]) == 4:
element.replace(words, "XXXX")
i = i+1
alist.append(newwords)
else:
alist.append(words)
print (alist)
file.close()
file1 = open(filename, 'w')
for element in alist:
file.write(alist)
file.close()
|