-2

我正在尝试编写一个程序来打开一个文本文档并用* * 替换所有四个字母的单词。我已经在这个程序上玩了好几个小时了。我似乎无处可去。我希望有人能够帮助我解决这个问题。这是我到目前为止所拥有的。非常感谢您的帮助!

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()
4

4 回答 4

2
for element in file:
    words = element.split()
    for word in words:
        if len(word) == 4:
            etc etc

原因如下:

假设文件中的第一行是“你好,我的名字是 john”,那么对于循环的第一次迭代:element = 'hello, my name is john'words = ['hello,','my','name','is','john']

您需要检查每个单词里面的内容for word in words

另外值得注意的是,在您当前的方法中,您没有注意标点符号。注意words上面的第一个词...

为了摆脱标点符号,不如说:

import string

blah blah blah ...
for word in words:
    cleaned_word = word.strip(string.punctuation)
    if len(cleaned_word) == 4:
       etc etc
于 2012-11-08T07:22:58.703 回答
1

这里有一个提示:len(words)返回当前行的单词数,而不是任何特定单词的长度。您需要添加代码来查看行中的每个单词并决定是否需要替换它。

此外,如果文件比简单的单词列表更复杂(例如,如果它包含需要保留的标点符号),则可能值得使用正则表达式来完成这项工作。

于 2012-11-08T07:19:03.117 回答
0

它可以是这样的:

def censor():
    filename = input("Enter name of file: ")
    with open(filename, 'r') as f:
        lines = f.readlines()

    newLines = []
    for line in lines:
        words = line.split()
        for i, word in enumerate(words):
            if len(word) == 4:
                words[i] == '**'
        newLines.append(' '.join(words))

    with open(filename, 'w') as f:
        for line in newLines:
            f.write(line + '\n')
于 2012-11-08T07:22:25.603 回答
0
def censor(filename):
"""Takes a file and writes it into file censored.txt with every 4-letterword replaced by xxxx"""
infile = open(filename)
content = infile.read()
infile.close()
outfile = open('censored.txt', 'w')
table = content.maketrans('.,;:!?', '      ')
noPunc = content.translate(table) #replace all punctuation marks with blanks, so they won't tie two words together
wordList = noPunc.split(' ')
for word in wordList:
    if '\n' in word:
        count = word.count('\n')
        wordLen = len(word)-count
    else:
        wordLen = len(word)
    if wordLen == 4:
        censoredWord = word.replace(word, 'xxxx ')
        outfile.write(censoredWord)
    else:
        outfile.write(word + ' ')
outfile.close()
于 2018-03-28T10:20:11.823 回答