Json文件转Excel表格
这里写自定义目录标题 样本数据样本数据说明转换执行转换转换效果 样本数据 {"_id":"AXtapGbijcEAuoI0lDsp","_index":"
这里写自定义目录标题
样本数据
{"_id":"AXtapGbijcEAuoI0lDsp","_index":"person_address_label_info_master","_score":null,"_source":{"AGE":48,"BIRTHDAY":"1973","BPLACE":"吉林省吉林市磐石县","IDNO":"xxxxx","IDTYPE":"01","QUERY_STRING":" 吉林省吉林市磐石县 48 73 1973 ","RNAME":"崔太花","SEX":"女"},"_type":"a","sort":[6754190]}
{"_id":"AXtapGbijcEAuoI0lDsp","_index":"person_address_label_info_master","_score":null,"_source":{"AGE":48,"BIRTHDAY":"1973","BPLACE":"吉林省吉林市磐石县","IDNO":"xxxxx","IDTYPE":"01","QUERY_STRING":" 吉林省吉林市磐石县 48 73 1973 ","RNAME":"崔太花","SEX":"女"},"_type":"a","sort":[6754190]}
{"_id":"AXtapGbijcEAuoI0lDsp","_index":"person_address_label_info_master","_score":null,"_source":{"AGE":48,"BIRTHDAY":"1973","BPLACE":"吉林省吉林市磐石县","IDNO":"xxxxx","IDTYPE":"01","QUERY_STRING":" 吉林省吉林市磐石县 48 73 1973 ","RNAME":"崔太花","SEX":"女"},"_type":"a","sort":[6754190]}
{"_id":"AXtapGbijcEAuoI0lDsp","_index":"person_address_label_info_master","_score":null,"_source":{"AGE":48,"BIRTHDAY":"1973","BPLACE":"吉林省吉林市磐石县","IDNO":"xxxxx","IDTYPE":"01","QUERY_STRING":" 吉林省吉林市磐石县 48 73 1973 ","RNAME":"崔太花","SEX":"女"},"_type":"a","sort":[6754190]}
{"_id":"AXtapGbijcEAuoI0lDsp","_index":"person_address_label_info_master","_score":null,"_source":{"AGE":48,"BIRTHDAY":"1973","BPLACE":"吉林省吉林市磐石县","IDNO":"xxxxx","IDTYPE":"01","QUERY_STRING":" 吉林省吉林市磐石县 48 73 1973 ","RNAME":"崔太花","SEX":"女"},"_type":"a","sort":[6754190]}
样本数据说明
- json文件每行是json格式,但json格式不标准,需要额外处理
- json文件整体不是标准json数组,需解析后转成json数组再转excel文件
转换
import json
import pandas as pd
import openpyxlf = open("files/公民信息.json", "r", encoding="utf-8")output_data = []
while True:line = f.readline()if not line:breakline = line.replace('"{', '{')line = line.replace('}"', '}')json_dict = json.loads(line)output_data.append({"姓名": json_dict['_source']['RNAME'],"性别": json_dict['_source']['SEX'],"年龄": json_dict['_source']['AGE'],"出生年份": json_dict['_source']['BIRTHDAY'],"身份证号": json_dict['_source']['IDNO'],"地址": json_dict['_source']['QUERY_STRING'],})print(output_data)
df = pd.DataFrame(output_data)
df.to_excel("公民信息.xlsx")
print(output_data)
执行转换
第一步:首先安装脚本所需依赖库
pip3 install pandas
pip3 install openpyxl第二步:其次把3个全量的需要转换的json文件替换到files目录里面
公民信息转表格,执行python3 converter_information.py然后会生成“公民信息.xlsx”
转换效果

版权声明
本文来自互联网用户投稿,文章观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处。如若内容有涉嫌抄袭侵权/违法违规/事实不符,请点击 举报 进行投诉反馈!