Commit c31bcc0e authored by Robin Mueller's avatar Robin Mueller
Browse files

added csv writer to core

parent 5ed4a2ae
#! /usr/bin/python3
CSV Writer
This class writes tables to a csv.
R. Mueller
from utility import mib_globals as g
from utility.mib_file_management import copy_file, move_file
# TODO: Export to SQL
class CsvWriter:
def __init__(self, filename, table_to_print=None, header_array=None):
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
def write_to_csv(self):
file = open(self.filename, "w")
file.write("Index" + self.fileSeparator)
for index in range(self.columnNumbers):
# noinspection PyTypeChecker
if index < len(self.headerArray)-1:
file.write(self.headerArray[index] + self.fileSeparator)
file.write(self.headerArray[index] + "\n")
for index, entry in self.tableToPrint.items():
file.write(str(index) + self.fileSeparator)
for columnIndex in range(self.columnNumbers):
# noinspection PyTypeChecker
if columnIndex < len(self.headerArray) - 1:
file.write(str(entry[columnIndex]) + self.fileSeparator)
file.write(str(entry[columnIndex]) + "\n")
def copy_csv(self, copy_destination: str = g.copyDestination):
copy_file(self.filename, copy_destination)
print("CSV file was copied to " + copy_destination)
def move_csv(self, move_destination):
move_file(self.filename, move_destination)
if move_destination == ".." or move_destination == "../":
print("CSV Writer: CSV file was moved to parser root directory")
print("CSV Writer: CSV file was moved to " + move_destination)
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