I want to delete and replace function on a CSV file only by using os and sys module (version is python 3).
I've tried this for replace function:
file = "test.csv"
keyword = "blah"
old = "the old word"
new = "the new word"
with open(file, "r") as f:
lines = f.readlines()
with open(file, "w") as f:
for line in lines:
if keyword in line:
line.replace(old, new)
and this for delete function:
file = "test.csv"
keyword = "blahblah"
with open(file, "r") as f:
lines = f.readlines()
with open(file, "w") as f:
for line in lines:
if line.strip("\n") != keyword:
f.write(line)