我正在尝试编写一个程序来打开一个文本文档并用* * 替换所有四个字母的单词。我已经在这个程序上玩了好几个小时了。我似乎无处可去。我希望有人能够帮助我解决这个问题。这是我到目前为止所拥有的。非常感谢您的帮助!
def censor():
filename = input("Enter name of file: ")
file = open(filename, 'r')
file1 = open(filename, 'w')
for element in file:
words = element.split()
if len(words) == 4:
file1 = element.replace(words, "xxxx")
alist.append(bob)
print (file)
file.close()
这是修改后的版本,我不知道这是否更好
def censor():
filename = input("Enter name of file: ")
file = open(filename, 'r')
file1 = open(filename, 'w')
i = 0
for element in file:
words = element.split()
for i in range(len(words)):
if len(words[i]) == 4:
file1 = element.replace(i, "xxxx")
i = i+1
file.close()