I need to import excel to a dictionary and then using the dictionary to find and replace strings in all SQL files in a folder.
I tried the below Python code to import from excel to the dictionary.
Excel input
import pandas as pd
d = pd.read_excel(r"findrep.xls",index_col=[0]).to_dict(orient='index')
print(d)
output for the code is coming as
{'art': {'rep': 'artist'}, 'car': {'rep': 'cart'}, 'dar': {'rep': 'dart'}}
but I need as follows
{'src':'rep','art':'artist','car':'cart','dar':'dart'}
then Also I need to replace all the files in a folder, I am trying to use the following code, but I am getting 'dict' object has no attribute 'iteritems' error.
please help..
import os
os.chdir("C:\\Users\\Magesh\\Documents\\Python Scripts\\sql")
replacements = {'src':'rep','art':'artist','car':'cart','dar':'dart'}
for files in os.listdir("."):
fo = open(files, "rU")
text = fo.read()
for src, target in replacements.iteritems():
text = text.replace(src, target)
fo.seek(0)
fo.write(text)
fo.truncate()
fo.close()