sahil parvani
sahil parvani

Reputation: 1

How to Remove the Unicode Signature from while converting a file from CSV to JSON?

So, I tried converting a file from Kaggle which was in CSV to JSON.

Which made a new JSON file, but the first field of each object had the \ufeff Unicode signature.

Below mentioned is the code I used to convert the file.

import csv 
import json 

def csv_to_json(csvFilePath, jsonFilePath):
    jsonArray = []
      
    #read csv file
    with open(csvFilePath, 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'Sport car price.csv'
jsonFilePath = r'Sport car price.json'
csv_to_json(csvFilePath, jsonFilePath)

Output :

[
    {
        "\ufeffCar Make": "Porsche",
        "Car Model": "911",
        "Year": "2022",
        "Engine Size (L)": "3",
        "Horsepower": "379",
        "Torque (lb-ft)": "331",
        "0-60 MPH Time (seconds)": "4",
        "Price (in USD)": "101,200"
    },
    {
        "\ufeffCar Make": "Lamborghini",
        "Car Model": "Huracan",
        "Year": "2021",
        "Engine Size (L)": "5.2",
        "Horsepower": "630",
        "Torque (lb-ft)": "443",
        "0-60 MPH Time (seconds)": "2.8",
        "Price (in USD)": "274,390"
    }
]

Upvotes: 0

Views: 16

Answers (0)

Related Questions