LENGTHS_TO_CENSOR = {4, 5}
CENSOR_CHAR = '*'
CENSOR_EXT = "-censored"
def replace_inner(word, char):
if len(word) < 3:
return word
return word[0] + char * len(word[1:-1]) + word[-1]
def create_censor_file(filename):
output_file = open(filename + CENSOR_EXT, "w+")
with open(filename) as source:
for line in source:
idx = 0
while idx < len(line):
# If the character isn't a letter, write it to the output file.
if not line[idx].isalpha():
output_file.write(line[idx])
idx += 1
else:
word = ""
while idx < len(line) and line[idx].isalpha():
word += line[idx]
idx += 1
if len(word) in LENGTHS_TO_CENSOR:
word = replace_inner(word, CENSOR_CHAR)
output_file.write(word)
output_file.close()
def main():
filename = input("File to be censored: ")
create_censor_file(filename)
if __name__ == "__main__":
main()
I was assigned a task to censor words that are length n
in a file. This file can potentially contain punctuation and numbers.
I originally tackled the problem by splitting the line into a list of words (using .split(' ')
) and checking the length to determine if the program should censor the word or not. This failed for inputs such as:
does not work.for.this.input
or.this
The output file must be exactly like the input but with words of length in LENGTHS_TO_CENSOR
censored with CENSOR_CHAR
.
I decided to abandon trying to make it Pythonic and ended up with this result. I want to know if there is a way to take this method and make it more Pythonic.