bard ofshipping
bard ofshipping

Reputation: 61

Python: How to go through a file and replace curse words with a "censored"

Basically, I want a script that opens a file, and then goes through the file and sees if the file contains any curse words. If a line in the file contains a curse word, then I want to replace that line with "CENSORED". So far, I think I'm just messing up the code somehow because I'm new to Python:

filename = input("Enter a file name: ")
censor = input("Enter the curse word that you want censored: ")

with open(filename)as fi:
    for line in fi:
        if censor in line:
            fi.write(fi.replace(line, "CENSORED"))
    print(fi) 

I am new to this, so I'm probably just messing something up...

Line, as in This code (if "Hat" was a curse word):

There Is 
A 
Hat

Would be:

There Is 
A 
CENSORED 

Upvotes: 2

Views: 2188

Answers (5)

jedwards
jedwards

Reputation: 30230

Consider:

filename = input("Enter a file name: ")
censor = input("Enter the curse word that you want censored: ")

# Open the file, iterate through the lines and censor them, storing them in lines list
with open(filename) as f:
    lines = [line.replace(censor, 'CENSORED').strip() for line in f]

# If you want to re-write the censored file, re-open it, and write the lines
with open(filename, 'w') as f:
    f.write('\n'.join(lines))

We're using a list comprehension to censor the lines of the file.

If you want to replace the entire line, and not just the word, replace

lines = [line.replace(censor, 'CENSORED').strip() for line in f]

with

lines = ['CENSORED' if censor in line else line.strip() for line in f]

Upvotes: 2

Martijn Pieters
Martijn Pieters

Reputation: 1123410

You cannot write to the same file your are reading, for two reasons:

  • You opened the file in read-only mode, you cannot write to such a file. You'd have to open the file in read-write mode (using open(filename, mode='r+')) to be able to do what you want.

  • You are replacing data as you read, with lines that are most likely going to be shorter or longer. You cannot do that in a file. For example, replacing the word cute with censored would create a longer line, and that would overwrite not just the old line but the start of the next line as well.

You need to write out your changed lines to a new file, and at the end of that process replace the old file with the new.

Note that your replace() call is also incorrect; you'd call it on the line:

line = line.replace(censor, 'CENSORED')

The easiest way for you to achieve what you want is to use the fileinput module; it'll let you replace a file in-place, as it'll handle writing to another file and the file swap for you:

import fileinput

filename = input("Enter a file name: ")
censor = input("Enter the curse word that you want censored: ")

for line in fileinput.input(filename, inplace=True):
    line = line.replace(censor, 'CENSORED')
    print(line, end='')

The print() call is a little magic here; the fileinput module temporarily replaces sys.stdout meaning that print() will write to the replacement file rather than your console. The end='' tells print() not to include a newline; that newline is already part of the original line read from the input file.

Upvotes: 5

Mark Tolonen
Mark Tolonen

Reputation: 177901

You have only opened the file for reading. Some options:

  • Read the whole file in, do the replacement, and write it over the original file again.
  • Read the file line-by-line, process and write the lines to a new file, then delete the old file and rename in the new file.
  • Use the fileinput module, which does all the work for you.

Here's an example of the last option:

import fileinput,sys
for line in fileinput.input(inplace=1):
    line = line.replace('bad','CENSORED')
    sys.stdout.write(line)

And use:

test.py file1.txt file2.txt file3.txt

Each file will be edited in place.

Upvotes: 0

Malik Brahimi
Malik Brahimi

Reputation: 16711

with open('filename.txt', 'r') as data:
    the_lines = data.readlines()

with open('filename.txt', 'w') as data:
    for line_content in the_lines:

        if curse_word in line_content:
            data.write('Censored')

        else:
            data.write(line_content)

Upvotes: 0

rutsky
rutsky

Reputation: 4110

filename = input("Enter a file name: ")
censor = input("Enter the curse word that you want censored: ")

with open(filename)as fi:
    for line in fi:
        if censor in line:
            print("CENSORED")
        else:
            print(line)

Upvotes: 0

Related Questions