I have a set of .csv files with ; delimiter. There are certain junk values in the data that I need to replace with blanks. A sample problem row is:
103273;CAN D MAT;B.C.;B.C.;B.C.;03-Apr-2006
Desired row after find and replace is:
103273;CAN D MAT;;;;03-Apr-2006
In the above example I'm replacing ;B.C.; with ;;
I cannot replace just B.C. as I need to match the entire cell value for this particular error case. The code that I am using is:
import os, fnmatch
def findReplace(directory, filePattern):
for path, dirs, files in os.walk(os.path.abspath(directory)):
for filename in fnmatch.filter(files, filePattern):
filepath = os.path.join(path, filename)
with open(filepath) as f:
s = f.read()
for [find, replace] in zip([';#DIV/0!;',';B.C.;'],[';;',';;']
s = s.replace(find, replace)
with open(filepath, "w") as f:
f.write(s)
findReplace(*Path*, "*.csv")
The output that I'm instead getting is:
103273;CAN D MAT;;B.C.;;03-Apr-2006
Can someone please help with this issue?
Thanks in advance!
#DIV/0!andB.C.with `` (empty string). Why not just do that? With straight forward approach.'103273;CAN D MAT;;;;;;;03-Apr-2006'for the example input, which is different from what you wrote.remodule is an overkill in this case. Splitting the string with ; and replacing the entire cell value with the empty string is better.