I am trying to remove a particular text from a CSV file. If it matches, I want it to remove the string and create a new csv file with the same data, but without the text.
If it doesn't I want it to not create a new CSV file and also print "NOPE".
My current script removes the text from the original CSV file if it matches, and creates a new CSV file. If the text I enter is incorrect, it creates a new CSV file, but doesn't change anything or print out my error message.
Here is my current code:
import csv
result = input("Enter the text you want to remove from the CSV: ")
input_file = open('Input.csv', 'r')
output_file = open('Output.csv', 'w', newline='')
data = csv.reader(input_file)
writer = csv.writer(output_file)
def DEL():
try:
for line in data:
line = [value.replace(result, '') for value in line]
writer.writerow(line)
except Exception:
print('NOPE')
DEL()