-->

Replace four letter word in python

2019-01-20 20:55发布

问题:

I am trying to write a program that opens a text document and replaces all four letter words with **. I have been messing around with this program for multiple hours now. I can not seem to get anywhere. I was hoping someone would be able to help me out with this one. Here is what I have so far. Help is greatly appreciated!

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

here is revised verison, i don't know if this is much better

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

回答1:

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

Here's why:

say the first line in your file is 'hello, my name is john' then for the first iteration of the loop: element = 'hello, my name is john' and words = ['hello,','my','name','is','john']

You need to check what is inside each word thus for word in words

Also it might be worth noting that in your current method you do not pay any attention to punctuation. Note the first word in words above...

To get rid of punctuation rather say:

import string

blah blah blah ...
for word in words:
    cleaned_word = word.strip(string.punctuation)
    if len(cleaned_word) == 4:
       etc etc


回答2:

Here is a hint: len(words) returns the number of words on the current line, not the length of any particular word. You need to add code that would look at every word on your line and decide whether it needs to be replaced.

Also, if the file is more complicated than a simple list of words (for example, if it contains punctuation characters that need to be preserved), it might be worth using a regular expression to do the job.



回答3:

It can be something like this:

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')


回答4:

You need replace the letter w in w+ on line 4: file1 = open(filename, 'w') haves to be file1 = open(filename, 'w+') (I was working on this project too so this was my code)

import os
import time
localtime = time.asctime( time.localtime(time.time()) )
a = input("What is your first name?   ").title()
b = input("And your last name?   ").title()
c = input("What is your e-mail adderess?   ")
d = input("And your phone number?   ")
f = input("When were you born? (02-01-1900 is 1 February 1900)")
print(a)
print(b)
print(c)
print(d)
print(f)
e = input("Is this correct? (Y\N)  ")
g = (a+"-"+b)




if e == "Y" or "y":
    new_path = '/users/Pivo/registreren/%s.txt' % g
    new_days = open(new_path,'w+')
    new_days.write(localtime)
    new_days.write('\n')
    new_days.write(a)
    new_days.write(" ")
    new_days.write(b)
    new_days.write('\n')
    new_days.write(c)
    new_days.write('\n')
    new_days.write(d)
    new_days.write('\n')
    new_days.write(f)
    new_days.write('\n')
    new_days.write('\n')
    new_days.write('\n')
    new_days.write('\n')
    new_days.close()
    print("Okay, done!")
if e == "N" or "n":
    os.startfile("C:/Users/Pivo/registreren/registreren.py")


回答5:

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