I am having trouble in dynamically mapping the dictionary values while writing the db output into a file. Scenario:
new_list = [{'Table':'A', 'Column':'C1', 'DataType':'int'},
{'Table':'A', 'Column':'C2', 'DataType':'varchar'},
{'Table':'A', 'Column':'C2', 'DataType':'numeric'}
]
# I want to write the data into a file.
Table|Column|DataType
A|C1|int
A|C2|varchar
A|C3|numeric
I am trying to do like below.
header = []
with open('my_log.log', 'w',encoding='utf-8') as log:
for n in new_list:
for i in n.keys():
header.append(i)
log.write("|".join(set(header)))
log.write("\n")
for data in new_list:
# don't want to hard code the keys like below
log.write("{Table}|{Column}|{DataType} \n".format(**data))
# need to do something so that I dont have to hard code the keys as it is dynamic in nature
# and also my file output should match with the header generated in the previous line
log.write("{???}".format(**data))
Any Suggestion!
json.dumpmore information can be found within Python's JSON documentation.csv.DictWriteris the "one...obvious way to do it"