Skip to content

Instantly share code, notes, and snippets.

@jugmac00
Created October 25, 2018 08:43
Show Gist options
  • Save jugmac00/fe1161d3e04d578e5191b50d8e4668d1 to your computer and use it in GitHub Desktop.
Save jugmac00/fe1161d3e04d578e5191b50d8e4668d1 to your computer and use it in GitHub Desktop.
import codecs
import cStringIO
import csv
class CsvConverter(object):
"""Convert a given list into CSV.
CsvConverter takes a data structure (list of iterables), and converts
it into a CSV.
You can pass in an optional argument header."""
def __init__(self, data, header=None):
self.data = data
self.header = header
def convert_to_csv(self):
"""Converts the given input into a csv file as string io."""
rows = []
if self.header:
rows.append(self.header)
rows.extend(self.data)
output = cStringIO.StringIO()
output.write(codecs.BOM_UTF8)
writer = csv.writer(output, dialect='excel-tab', delimiter=';', quotechar='"', quoting=csv.QUOTE_ALL)
for row in rows:
writer.writerow(row)
output.seek(0)
return output
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment