python from csv to dict code example
Example 1: python dictionary to csv
import csv
csv_columns = ['word','matchin']
csv_file = "found.csv"
try:
with open(csv_file, 'w') as csvfile:
writer = csv.DictWriter(csvfile, fieldnames=csv_columns)
writer.writeheader()
for data in corpus:
writer.writerow(data)
except IOError:
print("I/O error")
Example 2: read csv and store in dictionary python
import csv
with open('coors.csv', mode='r') as infile:
reader = csv.reader(infile)
with open('coors_new.csv', mode='w') as outfile:
writer = csv.writer(outfile)
mydict = {rows[0]:rows[1] for rows in reader}
Example 3: how to create dictionary in python from csv
input_file = csv.DictReader(open("coors.csv"))