10 examples of 'dict to csv python' in Python

Every line of 'dict to csv python' code snippets is scanned for vulnerabilities by our powerful machine learning engine that combs millions of open source libraries, ensuring your Python code is secure.

All examples are scanned by Snyk Code

By copying the Snyk Code Snippets you agree to
this disclaimer
53def row2_dict2csv(raw_dict = {}, csv_file = ""):
54 with open(csv_file,'w') as f:
55 w = csv.writer(f)
56 for k,v in raw_dict.items():
57 w.writerows([k,v])
Important

Use secure code every time

Secure your code as it's written. Use Snyk Code to scan source code in minutes – no build needed – and fix issues immediately. Enable Snyk Code

290def dict_to_csv(data=None, filename='test.csv'):
291 r'''
292 Utility function to write a dictionary to a csv file
293 Uses dictionary keys as column headers
294 Data should all be same length
295 '''
296 data_list = [data[key] for key in data.keys()]
297 zipper = list(zip(*data_list))
298 with open(filename, 'w') as f:
299 w = csv.writer(f, lineterminator='\n')
300 w.writerow(data.keys())
301 w.writerows(zipper)
305def to_csv(self, f):
306 is_path = False
307 if isinstance(f, str):
308 is_path = True
309 f = open(f, mode='w')
310 writer = csv.DictWriter(f, self.columns)
311 writer.writerows(self._data)
312 if is_path:
313 f.close()
4def dictionary_list_to_csv(dict_list):
5 keys = dict_list[0].keys()
6 csvfile = get_csv_file_name()
7 print("Gravando o arquivo " + csvfile)
8 with open(csvfile, "w") as output:
9 writer = csv.DictWriter(output, fieldnames=keys, lineterminator='\n')
10 writer.writeheader()
11 writer.writerows(dict_list)
55def to_csv(self):
56 """See :meth:`wader.common.interfaces.IContact.to_csv`"""
57 name = '"%s"' % self.name
58 number = '"%s"' % self.number
59 return [name, number]
25def write_dict_data_to_csv_file(csv_file_path, dict_data):
26 csv_file = open(csv_file_path, 'wb')
27 writer = csv.writer(csv_file, dialect='excel')
28
29 headers = dict_data[0].keys()
30 writer.writerow(headers)
31
32 # headers must be the same with dat.keys()
33 for dat in dict_data:
34 line = []
35 for field in headers:
36 line.append(dat[field])
37 writer.writerow(line)
38
39 csv_file.close()
151def as_csv(self):
152 return [[self.tract_id]]
90def write_rows_to_csv(row_list, filename):
91 """
92 Write a list of namedtuple objects to a (unicode) CSV file.
93
94 Args:
95 row_list A list of namedtuple objects
96 Returns:
97 Raises:
98 """
99 with open(filename,'w') as out_file:
100 csv_file = UnicodeWriter(out_file, quoting=csv.QUOTE_ALL)
101 write_header = True
102 for row in row_list:
103 if write_header:
104 write_header = False
105 csv_file.writerow(list(row._fields))
106 csv_file.writerow([x for x in row._asdict().itervalues()])
12def csv_to_json(mapping_name, csv_file):
13 reader = csv.reader(csv_file)
14 reader.next() # skip header row
15 mappings[mapping_name] = {}
16 for row in reader:
17 # print row
18 if row[1] != '' and row[2] != '':
19 mappings[mapping_name][row[0]] = {
20 'key1': row[1],
21 'val1': row[2]
22 }
23 if len(row) == 5:
24 if row[3] != '' and row[4] != '':
25 mappings[mapping_name][row[0]] = {
26 'key2': row[3],
27 'val2': row[4]
28 }
29 if len(row) == 7:
30 if row[5] != '' and row[6] != '':
31 mappings[mapping_name][row[0]] = {
32 'key3': row[4],
33 'val3': row[5]
34 }
67def write_csv(writer, data):
68 writer.writerow(data)

Related snippets