Unverified Commit 7717b36e authored by Robin Mueller's avatar Robin Mueller
Browse files

csv writer fix

parent 6ed4d76e
......@@ -11,39 +11,38 @@
@date
14.11.2019
"""
from utility import mib_globals as g
from modgen.utility.file_management import copy_file, move_file
# TODO: Export to SQL
class CsvWriter:
def __init__(self, filename, table_to_print=None, header_array=None):
def __init__(self, filename, table_to_print=None, header_array=None, file_separator: str = ","):
if header_array is None:
header_array = []
if table_to_print is None:
table_to_print = dict()
self.filename = filename
self.tableToPrint = table_to_print
self.headerArray = header_array
if self.headerArray != 0:
self.columnNumbers = len(self.headerArray)
self.fileSeparator = g.fileSeparator
self.table_to_print = table_to_print
self.header_array = header_array
if self.header_array != 0:
self.columnNumbers = len(self.header_array)
self.file_separator = file_separator
def write_to_csv(self):
file = open(self.filename, "w")
file.write("Index" + self.fileSeparator)
file.write("Index" + self.file_separator)
for index in range(self.columnNumbers):
# noinspection PyTypeChecker
if index < len(self.headerArray)-1:
file.write(self.headerArray[index] + self.fileSeparator)
if index < len(self.header_array)-1:
file.write(self.header_array[index] + self.file_separator)
else:
file.write(self.headerArray[index] + "\n")
for index, entry in self.tableToPrint.items():
file.write(str(index) + self.fileSeparator)
file.write(self.header_array[index] + "\n")
for index, entry in self.table_to_print.items():
file.write(str(index) + self.file_separator)
for columnIndex in range(self.columnNumbers):
# noinspection PyTypeChecker
if columnIndex < len(self.headerArray) - 1:
file.write(str(entry[columnIndex]) + self.fileSeparator)
if columnIndex < len(self.header_array) - 1:
file.write(str(entry[columnIndex]) + self.file_separator)
else:
file.write(str(entry[columnIndex]) + "\n")
file.close()
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment