如何在 Python 中将 JSON 转换为 XLS

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15379178/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 19:58:21  来源:igfitidea点击:

How to convert JSON to XLS in Python

pythonjsonxls

提问by fakelbst

Does anyone know how can I convert JSON to XLS in Python?

有谁知道如何在 Python 中将 JSON 转换为 XLS?

I know that it is possible to create xlsfiles using the package xlwtin Python.

我知道可以xls使用xlwtPython 中的包创建文件。

What if I want convert a JSONdata convert to XLSfile directly?

如果我想直接将JSON数据转换为XLS文件怎么办?

Is there a way to archive this?

有没有办法存档这个?

采纳答案by GodMan

I usually use tablib for this use. Its pretty simple to use: https://pypi.python.org/pypi/tablib/0.9.3

我通常使用 tablib 来做这个用途。使用起来非常简单:https: //pypi.python.org/pypi/tablib/0.9.3

回答by Bruno Lopes

Using pandas (0.15.1) and openpyxl (1.8.6):

使用熊猫 (0.15.1) 和 openpyxl (1.8.6):

import pandas
pandas.read_json("input.json").to_excel("output.xlsx")

回答by gii96

If your json file is stored in some directory then,

如果您的 json 文件存储在某个目录中,则

import pandas as pd
pd.read_json("/path/to/json/file").to_excel("output.xlsx")

If you have your json within the code then, you can simply use DataFrame

如果代码中有 json,则可以简单地使用 DataFrame

json_file = {'name':["aparna", "pankaj", "sudhir", "Geeku"],'degree': ["MBA", "BCA", "M.Tech", "MBA"],'score':[90, 40, 80, 98]}
df = pd.DataFrame(json_file).to_excel("excel.xlsx")