简体   繁体   中英

Not able to generate a proper csv file from JSON using python

I am not able to generate a proper csv file using the below code. But when I query in individually, I am getting the desired result. Below is the my json file and code

{
"quiz": {
    "maths": {
      "q2": {
            "question": "12 - 8 = ?",
            "options": [
                "1",
                "2",
                "3",
                "4"
            ],
            "answer": "4"
        },
        "q1": {
            "question": "5 + 7 = ?",
            "options": [
                "10",
                "11",
                "12",
                "13"
            ],
            "answer": "12"
        }
        
    },
    "sport": {
        "q1": {
            "question": "Which one is correct team name in NBA?",
            "options": [
                "New York Bulls",
                "Los Angeles Kings",
                "Golden State Warriros",
                "Huston Rocket"
            ],
            "answer": "Huston Rocket"
        }
    }        
}

}

import json
import csv

# Opening JSON file and loading the data
# into the variable data
with open('tempjson.json', 'r') as jsonFile:
    data = json.load(jsonFile)
    flattenData=flatten(data)

employee_data=flattenData

# now we will open a file for writing
data_file = open('data_files.csv', 'w')

# create the csv writer object
csv_writer = csv.writer(data_file)

# Counter variable used for writing 
# headers to the CSV file
count = 0

for emp in employee_data:
    if count == 0:

        # Writing headers of CSV file
        header = emp
        csv_writer.writerow(header)
        count += 1

    # Writing data of CSV file
    #csv_writer.writerow(employee_data.get(emp))

data_file.close()

Once the above code execute, I get the information as below:

在此处输入图像描述

I am not getting it what I am doing wrong. I am flattenning my json file and then trying to change it to csv

You can manipulate the JSON easily with Pandas Dataframes and save it to a CSV. I'm not sure how your desired CSV should look like, but the following code generates a CSV with columns question, options, and answers. It generates an index column with the name of the quiz and the question number in an alphabetically ordered list (your JSON was unordered). The code below will also work when more different quizzes and questions are added.

Maybe converting it natively in Python is performance-wise better, but manipulation using Pandas makes it easier.

import pandas as pd

# create Pandas dataframe from JSON for easy manipulation
df = pd.read_json("tempjson.json")

# create result dataframe
df_result = pd.DataFrame()

# Get nested dict from each dataframe row
for index, row in df.iterrows():
    
    # Convert it into a new dataframe
    df_temp = pd.DataFrame.from_dict(df.loc[index]['quiz'], orient='index')
    # Add name of quiz to index
    df_temp.index = index + ' ' + df_temp.index
    # Append row result to final dataframe
    df_result = df_result.append(df_temp)
    
# Optionally sort alphabetically so questions are in order
df_result.sort_index(inplace=True)

# convert dataframe to CSV
df_result.to_csv('quiz.csv')

CSV

Update on request: Export to CSV using flattened JSON:

import json
import csv
from flatten_json import flatten
import pandas

# Opening JSON file and loading the data
# into the variable data
with open("tempjson.json", 'r') as jsonFile:
    data = json.load(jsonFile)
    flattenData=flatten(data)
    
    
df = pd.DataFrame.from_dict(flattenData, orient='index')    

# convert dataframe to CSV
df.to_csv('quiz.csv', header=False)    

Results in the following CSV (Not sure what your desired outcome is since you did not provide the desired result in your question).

csv扁平化输出

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM