I have a very large array with many rows and many columns (called "self.csvFileArray
") that is composed of rows that I've read from a CSV file, using the following code in a class that deals with CSV files...
with open(self.nounDef["Noun Source File Name"], 'rU') as csvFile:
for idx, row in enumerate(csv.reader(csvFile, delimiter=',')):
if idx == 0:
self.csvHeader = row
self.csvFileArray.append(row)
I have a very long dictionary of replacement mappings that I'd like to use for replacements...
replacements = {"str1a":"str1b", "str2a":"str2b", "str3a":"str3b", etc.}
I'd like to do this in a class method that looks as follows...
def m_globalSearchAndReplace(self, replacements):
# apply replacements dictionary to self.csvFileArray...
MY QUESTION: What is the most efficient way to replace strings throughout the array "self.csvFileArray
", using the "replacements
" dictionary?
NOTES FOR CLARIFICATION:
I took a look at this post but can't seem to get it to work for this case.
Also, I want to replace strings within words that match, not just entire words. So, working with a replacement mapping of "SomeCompanyName":"xyz", I may have a sentence like "The company SomeCompanyName has a patent for product called abcSomeCompanyNamedef." You'll notice that the string has to be replaced, twice, in the sentence... once as a whole word and once as an embedded string.