To read a CSV file and convert it to JSON in Python, you can use the csv and json modules that come with Python. Here's some example code:
main.py369 chars17 lines
In this code, we use the csv.DictReader
method to read data from the CSV file and store it as a list of dictionaries. We then use the json.dumps
method to convert the list of dictionaries to a JSON-formatted string. Finally, we write the JSON data to a file using the open
method with the 'w'
flag to open the file in write mode.
gistlibby LogSnag