5 examples of 'how to export dataframe to csv in python' in Python

Every line of 'how to export dataframe to csv in 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
15def export_set(dataset):
16 """Returns a TSV representation of Dataset."""
17
18 stream = StringIO()
19
20 if is_py3:
21 _tsv = csv.writer(stream, delimiter='\t')
22 else:
23 _tsv = csv.writer(stream, encoding=DEFAULT_ENCODING, delimiter='\t')
24
25 for row in dataset._package(dicts=False):
26 _tsv.writerow(row)
27
28 return stream.getvalue()
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

6def export_csv(modeladmin, request, queryset):
7 exporter = exporter_registry[queryset.model]
8 return exporter(queryset).to_csv_response()
57def _dataframe_to_txt(writer, dataframe):
58 encoding_writer = codecs.getwriter('utf-8')(writer)
59 for row in dataframe.iterrows():
60 encoding_writer.write("".join(row[1].tolist()))
61 encoding_writer.write('\n')
20def ToCsv(self,name='FileName.csv', DataToWrite=[
21 ["First", "Second", "Third"], ]):
22 #Write DataResult to CSV file
23 with open(name, 'w', newline='') as fp:
24 a = csv.writer(fp, delimiter=',')
25 a.writerows(DataToWrite)
29def export_as_csv(modeladmin, request, queryset):
30 model = modeladmin.model
31 response = HttpResponse(content_type='text/csv')
32 model_name = force_unicode(model._meta.verbose_name)
33 timestamp = datetime.now().strftime('%d%m%y_%H%M')
34 filename = '%s_export_%s.csv' % (model_name, timestamp)
35 response['Content-Disposition'] = 'attachment; filename=%s' % filename
36 writer = csv.writer(response)
37 excluded = getattr(modeladmin, 'export_excluded', [])
38 field_names = set(modeladmin.list_display) - set(excluded)
39
40 def get_label(field):
41 label = util.label_for_field(field, model, modeladmin)
42 return force_unicode(label).title().encode('utf-8')
43
44 def serialize(field, obj):
45 f, attr, value = util.lookup_field(field, obj, modeladmin)
46 if f is not None and not isinstance(f, models.BooleanField):
47 value = util.display_for_field(value, f)
48 return force_unicode(value).encode('utf-8')
49
50 header_row = [get_label(field) for field in field_names]
51
52 if queryset.count() > 0:
53 first = queryset[0]
54 if hasattr(first, 'export_related'):
55 header_row += first.export_related().keys()
56
57 writer.writerow(header_row)
58
59 for obj in queryset:
60 field_values = [serialize(field, obj) for field in field_names]
61
62 if callable(getattr(obj, 'export_related', False)):
63 field_values += obj.export_related().values()
64
65 if hasattr(obj, 'extradata'):
66 try:
67 data = ast.literal_eval(obj.extradata)
68 except SyntaxError:
69 data = {}
70 values = [v.encode('utf-8') for v in data.values()]
71 field_values += values
72
73 writer.writerow(field_values)
74
75 return response

Related snippets