In this tutorial I will show you how to convert csv to json in python.
To convert csv to json:
we have to import csv and json packages. Use csv.DictReader() to read the lines of the csv file and then add to the array. To convert object to the JSON string using json.dumps() and write to JSON String to json file.
import csvimport jsonjsonArr = with open('shampoo_sales.csv', encoding='utf-8') as csvfile:csvReader = csv.DictReader(csvfile)for row in csvReader:jsonArr.append(row)with open('shampoo_sales.json', 'w', encoding='utf-8') as jsonfile:jsonString = json.dumps(jsonArr, indent=4)jsonfile.write(jsonString)
Post your comments / questions
- How to fix HAXM is not installed |in Android Studio
- How to fix CMOS Checksum Error in Computer or Laptop | SOLVED
- Reactivating windows after a Hardware change on PC or Laptop
- FIXED: Windows reported that the hardware of your device has changed. Error code :0xc004F211
- "redirect" is not defined pylance("reportUndefinedVariable)
- This action cannot be completed because the file is open in SQL Server(SQLEXPRESS) - FIXED
- Unicode error 'unicodeescape' codec can't decode bytes in position 2-3: truncated UXXXXXXXX escape
- Could not find the 'angular-devkit/build-angular:dev-server' builder's node package | Angular Error