Skip to content

Instantly share code, notes, and snippets.

@hzw1199
Created August 11, 2023 07:01
Show Gist options
  • Save hzw1199/021a5953b6ef89b01f75fa9e482b37f9 to your computer and use it in GitHub Desktop.
Save hzw1199/021a5953b6ef89b01f75fa9e482b37f9 to your computer and use it in GitHub Desktop.
Convert JSON to EXCEL
# 导入pandas库
import pandas as pd
# 导入sys库,用来获取命令行参数
import sys
# 定义一个函数,用来将JSON文件转换成Excel文件
def convert_json_to_excel(json_file, excel_file):
# 使用read_json方法,读取JSON文件,并创建一个DataFrame对象
df = pd.read_json(json_file)
# 使用to_excel方法,将DataFrame对象保存成Excel文件
df.to_excel(excel_file)
# 打印成功信息
print(f"Successfully converted {json_file} to {excel_file}")
# 如果这个文件是作为主程序运行,就获取命令行参数,并调用转换函数
if __name__ == "__main__":
# 检查参数个数是否正确
if len(sys.argv) != 3:
print("Usage: python convert_json_to_excel.py <json_file> <excel_file>")
sys.exit(1)
# 获取参数值
json_file = sys.argv[1]
excel_file = sys.argv[2]
# 调用转换函数
convert_json_to_excel(json_file, excel_file)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment