0

I've multiple json that I want to convert into a single csv with pandas. Here's my code:

import json
import os
import pandas as pd

data = []

for file in os.listdir("json"):
    if file.endswith(".json"):
        print(file)
        data = json.load(open(os.path.join("json", file)))
        df = pd.json_normalize(data, 'items')
        print(df)
        data.append(df)
temp = pd.concat(data, ignore_index = True) #concat different dataframe
temp.to_csv("output.csv", index=False, sep='\t', encoding="utf-8") #write to csv file

But I get this error: data.append(df) AttributeError: 'dict' object has no attribute 'append'

What am I doing wrong? In all the documentation and examples I've studied it seems the right thing to do.

1 Answer 1

3

You're assigning data as a list:

data = []

...but then re-assigning it here (which is a dict):

data = json.load(open(os.path.join("json", file)))

Use a different variable name.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.