This example will tell you how to use python built-in json and csv module to convert a csv file to a json file, it also shows how to convert a json file to csv file.
This example will tell you how to use python built-in json and csv module to convert a csv file to a json file, it also shows how to convert a json file to csv file. The example files are listed in above picture. I will explain them below. CSVJSONConvertionExample.py: This is the python source code file.There define a JsonCsvConverter class in it. This class has three method, you can get each.
The example files are listed in above picture. I will explain them below.
- CSVJSONConvertionExample.py : This is the python source code file. There define a JsonCsvConverter class in it. This class has three method, you can get each method explanation in the method comments.
- csv_user_info.csv : This is the source csv file, only this file exist at beginning.
- json_user_info.json : This file is generated by the csv_2_json_by_reader or csv_2_json_by_dictreader method. The two method read csv data from csv_user_info.csv file and convert the data to python dictionary list object and then save the dict list object in this json file. The difference between the two method is the first method read the csv file use csv.reader object, the second method read the csv file use csv.DictReader object.
- new_csv_user_info.csv : This file is generated by the json_2_csv method.
CSVJSONConvertionExample.py
csv_user_info.csv
json_user_info.json
Reference