python中怎么替换字母_替换python中的四个字母单词
我正在尝试编写一个打开文本文档并用**替换所有四个字母单词的程序.我现在一直在搞乱这个程序多个小时.我似乎无法到达任何地方.我希望有人能够帮助我解决这个问题.这是我到目前为止所拥有的.非常感谢帮助!
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()
这里是修订版的verison,我不知道这是否好得多
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()
本文来自互联网用户投稿,文章观点仅代表作者本人,不代表本站立场,不承担相关法律责任。如若转载,请注明出处。 如若内容造成侵权/违法违规/事实不符,请点击【内容举报】进行投诉反馈!
