Skip to content

Instantly share code, notes, and snippets.

@bencleary
Last active January 27, 2018 20:41
Show Gist options
  • Save bencleary/b903cf682394dc01ce484be9840519b8 to your computer and use it in GitHub Desktop.
Save bencleary/b903cf682394dc01ce484be9840519b8 to your computer and use it in GitHub Desktop.
def csv_file_from_list(self, headers: list, data: list, single_record_indicator=False):
"""
Creates and stores a CSV, rather than use a queryset it uses a list
:param headers
:param data
:param single_record_indicator
:return: list
"""
file_path = os.path.join(os.path.abspath(os.path.dirname("__file__")), "store", "csv", self.file_name)
with open(file_path, 'w') as file:
writer = csv.DictWriter(file, fieldnames=headers, delimiter=',', lineterminator='\n')
writer.writeheader()
if single_record_indicator:
writer.writerow(model_to_dict(data[1]))
else:
for row in data:
writer.writerow(model_to_dict(row[1]))
return [self.file_name, file_path]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment