1

I am using a CSV to JSON converter where I am specifying both, input and output files to be UTF-8 format, however, the output file is corrupt. Below is a sample of data in CVS and in JSON:

CSV: "1993885","Rejean",,"Lacroix","gf.employee@xxxx.xxx","Rejean.Lacroix",,"15-B Principale nord","St-Bruno de Guigues (Quebec)",,"St-Bruno de Guigues (Quebec)","QC","CA","J0Z 2G0","Sorting, Packing and Stacking","Male","Prép.table triage automatique","Active","Employee",19491121,19760804

JSON: { "\ufeffEmployeeID": "1993885", "FirstName": "Rejean", "MiddleName": "", "LastName": "Lacroix", "PersonalEmailAddress": "gf.employee@xxxx.xxx\n\n", "LoginID": "Rejean.Lacroix", "": "Sorting, Packing and Stacking", "HomeAddressLine1": "15-B Principale nord", "HomeAddressLine2": "St-Bruno de Guigues (Quebec)", "HomeAddressLine3": "", "HomeAddressCity": "St-Bruno de Guigues (Quebec)\n\n\n", "HomeAddressState": "QC", "HomeAddressCountry": "CA", "HomeAddressPostalCode": "J0Z 2G0", "Gender": "Male", "PositionTitle": "Pr\u00e9p.table triage automatique", "Status": "Active", "Shift": "Employee\n\n", "DateOfBirth": "19491121", "DateOfHire": "19760804" }

Below is the code that I am using:

''' import csv import json import time

def csv_to_json(csvFilePath, jsonFilePath): jsonArray = []

#read csv file
with open(csvFilePath, 'r', encoding='utf-8') as csvf:
    #load csv file data using csv library's dictionary reader
    csvReader = csv.DictReader(csvf)

    #convert each csv row into python dict
    for row in csvReader:
        #add this python dict to json array
        jsonArray.append(row)

#convert python jsonArray to JSON String and write to file
with open(jsonFilePath, 'w', encoding='utf-8') as jsonf:
    jsonString = json.dumps(jsonArray, indent=4)
    jsonf.write(jsonString)

csvFilePath = r'scoopsoft_dump.csv' jsonFilePath = r'scoopsoft_dump.json'

start = time.perf_counter() csv_to_json(csvFilePath, jsonFilePath) finish = time.perf_counter()

print(f"Conversion 100.000 rows completed successfully in {finish - start:0.4f} seconds") '''

0 Answers0