本文主要是介绍使用python将json格式转换成excle,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
背景:
抓取数据是json格式内容,涉及敏感数据使用在线工具可能存在数据泄露风险,为确保数据安全本地转换较为安全
代码
import pandas as pd
import json
import datetime
import os
now = datetime.datetime.now()
formatted_time = now.strftime("%Y-%m-%d_%H-%M-%S")
# Read JSON data from a text file with permissive error handling
with open('1.txt', 'r', encoding='utf-8', errors='replace') as file:
json_data = json.load(file)
# Extract the 'data' part from JSON
data_list = json_data.get("data", [])
# Convert to DataFrame
df = pd.DataFrame(data_list)
df.to_excel(f"{formatted_time}.xlsx", index=False)
这篇关于使用python将json格式转换成excle的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!