I have a directory with hundreds of CSV files in it. Many of the files have synonyms for words that I no longer want. I want to replace all of the synonyms with approved words. For example some CSVs have synonyms like vial jug canteen urn etc and I want to replace all of those with the word bottle. How would I do this? I know I can use glob to go through all of the files and I found http://stackoverflow.com/questions/17536138/replace-multiple-strings-in-csv-file?lq=1 but that does not really do what I need.

Does anyone have any ideas?

The listdir() function of the os module returns a list of all files in a given directory.

If you combine that with the fileinput module's input() function, you can go over all the files in a given directory and replace whatever you wish.
(hint: look at the inplace optional argument of input()).


Good luck!

Just a hint ...

bottle = "vial jug canteen urn"
transport = "car automobile airplane scooter"

mydict = {}
for word in bottle.split():
    mydict[word] = 'bottle'

for word in transport.split():
    mydict[word] = 'transport'

#print(mydict)  # test

text_old = "he carried a jug of water and a canteen of brandy in his car"

text_new = ""
space = " "
for item in text_old.split():
    if item in mydict.keys():
        text_new += mydict[item] + space
        text_new += item + space


he carried a jug of water and a canteen of brandy in his car
he carried a bottle of water and a bottle of brandy in his transport 
This question has already been answered. Start a new discussion instead.