Home > database >  Ignore some json files during parsing
Ignore some json files during parsing

Time:11-21

I have the following code that reads some JSON files from a directory and returns them after some preprocessing. However, some of them are dict so they do not have the desired columns. As a result, I take back this error

KeyError: "None of [Index(['aaa', 'xxx'], dtype='object')] are in the [columns]"]

How to ignore them and continue with the other JSON files? Perhaps a try-except procedure?

import os, json
import pandas as pd

path_to_json = 'C:/Users/aaa/Desktop/'
json_files = [pos_json for pos_json in os.listdir(path_to_json) if pos_json.endswith('.json')]

def func(s):
    try:
        return eval(s)
    except:
        return dict()
list_of_df=[]
for i in range(len(json_files)):
    file_name = json_files[i]
    df = pd.read_json(file_name, lines=True)
    df= df[['columnx']]
    df = df['columnx'].apply(func)
    df=pd.json_normalize(df)
    df=pd.DataFrame(df[["xxx", "aaa"]])
    list_of_df.append(df)

df=pd.concat(list_of_df)
df = df[['Index','xxx', 'aaa']]
df.head()

CodePudding user response:

You have to add the try-except block added in your for loop which iterates over the json files.

  • Related