This example will tell you how to use python’s built-in json and csv modules to convert a CSV file to a JSON file, it also shows how to convert a JSON file to a CSV file.
The example files are listed in the 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 methods, you can get each method explanation in the method comments.
- csv_user_info.csv: This is the source CSV file, only this file exists at the 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 methods read CSV data from the csv_user_info.csv file and convert the data to the python dictionary list object and then save the dict list object in this JSON file.
- The difference between the two methods is the first method reads 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.
1. CSVJSONConvertionExample.py
- CSVJSONConvertionExample.py
''' @author: zhaosong ''' import csv import os import json class JsonCsvConverter(object): # Convert a csv file content to a json file. Use csv.reader to read csv file data. def csv_2_json_by_reader(self, csv_file, json_file): # If the source csv_file exist. if(os.path.exists(csv_file)): # Open the csv file object with read permission. csv_file_object = open(csv_file, 'r') # Create a csv.reader object. csv_reader = csv.reader(csv_file_object) # Save all csv file row data in a dictionary. Each row is another python dictionary. table_data_dict = {} # This list used to save csv field names. header_field_names_row = [] # Save csv field names count. header_field_names_length = 0 # Record the row number. row_number = 0 # Each row in the csv_reader is a list. for row in csv_reader: # This is the header field row. if(row_number==0): header_field_names_row = row header_field_names_length = len(row) else: # Save each row data in a dictionary. row_data_dict = {} # Loop the row get each column value and related field name. for i in range(header_field_names_length): # Get column field name. field_name = header_field_names_row[i] # Get column value. field_value = row[i] # Set column name and value in row_data_dict. row_data_dict[field_name] = field_value # Add the row dictionary object to the table dictionary object. table_data_dict[row_number] = row_data_dict row_number += 1 # Open the json file with write permission. json_file_object = open(json_file, 'w') # Dump the table_data_dict to json file. json.dump(table_data_dict, json_file_object) print("Convert " + csv_file + " to " + json_file + " success.") else: print(csv_file + " do not exist. ") # Convert a csv file content to a json file. Use csv.DictReader to read csv file data. def csv_2_json_by_dictreader(self, csv_file, json_file): # If the source csv_file exist. if(os.path.exists(csv_file)): # Open the csv file object with read permission. csv_file_object = open(csv_file, 'r') # Create a csv.reader object. dict_csv_reader = csv.DictReader(csv_file_object, ) # Save all csv row data in a dictionary. Each row is another python dictionary. table_data_dict = {} # Record the row number. row_number = 1 # Each row in the csv_reader is a dict list. for row in dict_csv_reader: # Set row data in table_data_dict. table_data_dict[row_number] = row row_number += 1 # Open the json file with write permission. json_file_object = open(json_file, 'w') # Dump the table_data_dict to json file. json.dump(table_data_dict, json_file_object) print("Convert " + csv_file + " to " + json_file + " success.") else: print(csv_file + " do not exist. ") # Convert a json file content to a csv file. def json_2_csv(self, json_file, csv_file): # If the source json_file exist. if(os.path.exists(json_file)): # Get a file object with write permission. json_file_object = open(json_file, 'r') # Load JSON file data to a python dict object. table_dict = json.load(json_file_object) header_field_names_list = [] # Convert the keys to a list. Otherwise when you call dict_keys[0] later, it will throw dict_keys do not support indexing error. dict_keys = list(table_dict.keys()) dict_keys_length = len(dict_keys) if(dict_keys_length > 0): # Get dict key. dict_key = dict_keys[0] # Get related value. row_dict = table_dict[dict_key] # Because the value is also a dictionary, so call it's keys() method to get a keys list, do not forget use list() to convert it to a pure list. # This list will be used as the csv file headers field names. header_field_names_list = list(row_dict.keys()) # Open csv file with write permission. csv_file_object = open(csv_file, 'w') # Create a csv.DictWriter object with the target csv file and specified header field names list.. csv_dict_file_writer = csv.DictWriter(csv_file_object, fieldnames=header_field_names_list) # Write csv file header field names. csv_dict_file_writer.writeheader() # Get rows dict list rows_dict_list = list(table_dict.values()) # Loop in the row list. for row_dict in rows_dict_list: # Write each row dict data to the csv file. csv_dict_file_writer.writerow(row_dict) print("Convert " + json_file + " to " + csv_file + " success.") else: print(json_file + " do not exist. ") if __name__ == '__main__': converter = JsonCsvConverter() #converter.csv_2_json("./csv_user_info.csv", "./json_user_info.json") #converter.csv_2_json_by_dictreader("./csv_user_info.csv", "./json_user_info.json") converter.json_2_csv("./json_user_info.json", "./new_csv_user_info.csv")
2. csv_user_info.csv
- csv_user_info.csv
'Name','Email','Title' 'jerry','[email protected]','CEO' 'tom','[email protected]','Developer'
3. json_user_info.json
- json_user_info.json
{"1": {"'Name'": "'jerry'", "'Email'": "'[email protected]'", "'Title'": "'CEO'"}, "2": {"'Name'": "'tom'", "'Email'": "'[email protected]'", "'Title'": "'Developer'"}}
Reference