有一个函数可以将 JSON 数据写入 Excel 文件:
import json
import pandas as pd
def Json_to_Excel(json_data):
pd.read_json(json_data, convert_axes=True).to_excel('file_name.xlsx', index=False)
json_data = [{'DateForm': '2024-01-12'},
{'GUID': '15f24792-3013-410b-97bd-cea4d08cf9c5'},
{'ListType': '4'},
{'Region': 'Такого тега нет в файле'},
{'ObjectsType': '002001002000'},
{'Categories': 'Такого тега нет в файле'}]
Json_to_Excel(json.dumps(json_data))
Json_to_Excel() 函数的输出有些出乎意料:
D:\Python\OKS16\OKS_16\OKS_16.py:149: FutureWarning: Passing literal json to 'read_json' is deprecated and will be removed in a future version. To read from a literal string, wrap it in a 'StringIO' object.
print(pd.read_json(json_data, convert_axes=True))
0 2024-01-12 ... NaN
1 NaN ... NaN
2 NaN ... NaN
3 NaN ... NaN
4 NaN ... NaN
5 NaN ... Такого тега нет в файле
[6 rows x 6 columns]
Process finished with exit code 0
如何更正代码,使数据写入一行而不出现 NaN
大概。你需要这样的东西: