I have several hundred csv files that I would like to search for the string "Keyed,Bet" and change it to "KeyedBet". The string may or may not be within the file, and may be in different columns in different files.
I cobbled together the script below, but it doesn't work. I am definitely using replace() incorrectly, but can't quite figure out how, and am creating a new file when I don't really need to- if it simply updated the current file and saved under the same name, that would be fine (but beyond my beginner capabilities).
Where did I go wrong here? Thanks for the help!
import os
import csv
path='.'
filenames = os.listdir(path)
for filename in filenames:
if filename.endswith('.csv'):
r=csv.reader(open(filename))
new_data = []
for row in r:
replace("Keyed,Bet","KeyedBet")
new_data.append(row)
newfilename = "".join(filename.split(".csv")) + "_edited3.csv"
with open(newfilename, "w") as f:
writer = csv.writer(f)
writer.writerows(new_data)
sedshell command (not python).sed -i 's/Keyed,Bet/KeyedBet/ig' *.csv