def testCsvGenerator(self): colNames, offsets, fixedWidthEncoding, includeHeader, delimitedEncoding = spec.loadSpec( "../spec.json") with open("../newTestOutput.csv",'r',encoding=delimitedEncoding) as fileCsv,\ open("../newTestInput.txt", 'r', encoding = fixedWidthEncoding) as fileTxt: firstLineCsv = fileCsv.readline()[:-1].split(',') # check headers. self.assertEqual(colNames, firstLineCsv) next(fileCsv) for csvLine, txtLine in zip(fileCsv, fileTxt): csvList = csvLine[:-1].split(',') indexStart = 0 indexEnd = 0 offsetIndex = 0 for csvCell in csvList: temp = int(offsets[offsetIndex]) indexEnd += temp txtCell = txtLine[indexStart:indexEnd] # check each cell in each line. self.assertEqual(csvCell, txtCell) indexStart = indexEnd offsetIndex += 1
def generateTestInput(inputFile, specFile, lines): with open(specFile, 'r') as spec: _, offsets, fixedWidthEncoding, _, _ = loadSpec(specFile) with open(inputFile, 'w', encoding=fixedWidthEncoding) as file: for num in range(lines): for offset in offsets: file.write('f' + (int(offset) - 1) * offset[0]) file.write('\n')
def testSpec(self): colNames, offsets, fixedWidthEncoding, includeHeader, delimitedEncoding = spec.loadSpec( '..//spec.json') offsetList = ['5', '12', '3', '2', '13', '7', '10', '13', '20', '13'] self.assertEqual(True, len(colNames) == len(offsets) == 10) for num in range(len(colNames)): self.assertEqual(f'f{num+1}', colNames[num]) self.assertEqual(offsetList[num], offsets[num]) self.assertEqual("windows-1252", fixedWidthEncoding) self.assertEqual('True', includeHeader) self.assertEqual('utf-8', delimitedEncoding)
def __init__(self,inputFile,outputFile,specFile): self.inputFile = inputFile self.outputFile = outputFile self.specFile = specFile self.colNames,self.offsets, _ ,self.includeHeader, _ = loadSpec(specFile)
import sys, logging from converter.specLoader import loadSpec from converter.converterToCsv import FixedWidthToCsv logging.basicConfig(filename='FixedWidthToCsv_Error.log', level=logging.ERROR, format='%(asctime)s:%(levelname)s:%(message)s') if __name__ == "__main__": if len(sys.argv) != 4: sys.exit( "Please input your command as : python main.py <inputFile> <outputFile.csv> <specFile.json>" ) inputFile = sys.argv[1] outputFile = sys.argv[2] specFile = sys.argv[3] try: colNames, offsets, fixedWidthEncoding, includeHeader, delimitedEncoding = loadSpec( specFile) with open(inputFile,'r',encoding=fixedWidthEncoding) as inputFile,\ open(outputFile,'w',encoding=delimitedEncoding, newline='') as outputFile: converter = FixedWidthToCsv(inputFile, outputFile, specFile) converter.toCsvFile() except: # if error, log it in the FixedWidthToCsv_Error.log logging.ERROR("Check what is going on")