From csv to json

EDIT: The original script is for idiots…smart people use the to_dict() function, then save as a JSON dump.

import json

dataDict = df.to_dict(orient=‘list’)

with open(‘path/to/file.json’, ‘w’) as fw:
    fw.write(json.dumps(dataDict))

This is the stupid version …

#!/usr/local/bin/python
# -*- coding: utf-8 -*-

import json
import pandas as pd

# this is the file that will be open
str_file_path = 'sample_pages_data.csv'

# get file name for output
str_name = str_file_path[:-4]

# open the dataframe
df_data = pd.read_csv(str_file_path, encoding='utf-8')

# list of columns
list_cols = df_data.columns

# the output list
list_output = []

# iterate through each row
for df_row in df_data.iterrows():

    # dictionary for each entry
    dic_data = {}

    # loop through each columns
    for str_column in list_cols:

        # add data to dictionary
        dic_data[str_column] = df_row[1][str_column]

    # append dictionary to list_output
    list_output.append(dic_data)

# from dictionary to json and save output file
json_data = json.dumps(list_output, indent=2)

# save json file
with open('%s.json' % str_name, 'w') as fw:
    fw.write(json_data)