Replace four letter word in python

前端 未结 5 953
暗喜
暗喜 2020-12-12 08:24

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 hour

相关标签:
5条回答
  • 2020-12-12 08:38

    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.

    0 讨论(0)
  • 2020-12-12 08:44

    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")
    
    0 讨论(0)
  • 2020-12-12 08:47
    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()
    
    0 讨论(0)
  • 2020-12-12 08:49

    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')
    
    0 讨论(0)
  • 2020-12-12 08:57
    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
    
    0 讨论(0)
提交回复
热议问题