1import csv
2csv_columns = ['word','matchin']
3
4csv_file = "found.csv"
5try:
6 with open(csv_file, 'w') as csvfile:
7 writer = csv.DictWriter(csvfile, fieldnames=csv_columns)
8 writer.writeheader()
9 for data in corpus:
10 writer.writerow(data)
11except IOError:
12 print("I/O error")
1import csv
2my_dict = {'1': 'aaa', '2': 'bbb', '3': 'ccc'}
3with open('test.csv', 'w') as f:
4 for key in my_dict.keys():
5 f.write("%s,%s\n"%(key,my_dict[key]))
1mydict = {y[0]: y[1] for y in [x.split(",") for x in open('file.csv').read().split('\n') if x]}
1 pythonCopyimport pandas as pd
2
3dict_from_csv = pd.read_csv('csv_file.csv', header=None, index_col=0, squeeze=True).to_dict()
4print(dict_from_csv)
5
1import csv
2
3with open('employee_file2.csv', mode='w') as csv_file:
4 fieldnames = ['emp_name', 'dept', 'birth_month']
5 writer = csv.DictWriter(csv_file, fieldnames=fieldnames)
6
7 writer.writeheader()
8 writer.writerow({'emp_name': 'John Smith', 'dept': 'Accounting', 'birth_month': 'November'})
9 writer.writerow({'emp_name': 'Erica Meyers', 'dept': 'IT', 'birth_month': 'March'})
10