在python中转换excel到json文件

yrdbyhpb  于 2023-02-15  发布在  Python
关注(0)|答案(2)|浏览(125)

我是新来的,需要一些帮助写入JSON文件:
我有一个具有以下值的 Dataframe ,它是通过阅读excel文件

创建的
需要将此写入json文件,对象为列dtl
输出:

wlwcrazw

wlwcrazw1#

在问题中考虑了类似的任务:
Converting Excel into JSON using Python
解决这一问题可以有不同的方法。

k0pti3hp

k0pti3hp2#

我希望,它对你的解决方案有效。

import pandas as pd
import json
df = pd.read_excel('./TfidfVectorizer_sklearn.xlsx')
df.to_json('new_file1.json', orient='records') # excel to json
# read json and then append details to it
with open('./new_file1.json', 'r') as json_file: 
    a = {}
    data = json.load(json_file)
    a['details'] = data
# write new json with details in it
with open("./new_file1.json", "w") as jsonFile:
    json.dump(a, jsonFile)

JSON输出:

相关问题