Python全面解析json数据并保存为csv文件

 更新时间:2022年07月06日 14:52:25   作者:Hello AI!  
这篇文章主要介绍了Python全面解析json数据并保存为csv文件,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教

解析json数据并保存为csv文件

首先导入两个包:

import json
import pandas as pd

打开json 文件并读取:

with open('2.json', encoding='utf-8') as f:
    line = f.readline()
    d = json.loads(line)
    f.close()

读取的json数据会以字典的形势保存,按照字典的读取方式获取自己想要的数据:

datas_x = []
datas_y = []
for dss in d:
    datas_x.append(float(dss["pos"]["x"]))
    datas_y.append(float(dss["pos"]["z"]))

将数据保存到列表中,然后创建pandas的DataFrame,DataFrame是由多种类型的列构成的二维标签数据结构。

path_x = pd.Series(datas_x)
path_y = pd.Series(datas_y)
path_df = pd.DataFrame()
path_df['pathx'] = path_x
path_df['pathy'] = path_y

最后将数据保存到csv中。

filepath = "E:\\python\\python\\2021\\202104\\0409\\path_data.csv"
path_df.to_csv(filepath, index=False, header=False)

完整代码

import json
import pandas as pd
filepath = "E:\\python\\python\\2021\\202104\\0409\\path_data.csv"
with open('2.json', encoding='utf-8') as f:
    line = f.readline()
    d = json.loads(line)
    f.close()
datas_x = []
datas_y = []
for dss in d:
    datas_x.append(float(dss["pos"]["x"]))
    datas_y.append(float(dss["pos"]["z"]))
path_x = pd.Series(datas_x)
path_y = pd.Series(datas_y)
path_df = pd.DataFrame()
path_df['pathx'] = path_x
path_df['pathy'] = path_y
path_df.to_csv(filepath, index=False, header=False)

将json任意行文件转为csv文件并保存

将json格式的前3000条数据存入csv

json格式类型:

{"address": "华山路31号", "addressExtend": "屯溪老街", "amenities": [1, 2, 3, 5, 10, 12], "brandName": null, "businessZoneList": null, "cityCode": 1004, "cityName": "黄山", "coverImageUrl": "https://img20.360buyimg.com/hotel/jfs/t16351/270/1836534312/106914/9b443bc4/5a68e68aN23bfaeda.jpg", "districtName": "屯溪区", "geoInfo": {"distance": 3669, "name": "市中心", "type": 1, "typeName": "市中心"}, "grade": 5, "hotelId": 328618, "location": {"lat": "29.717982", "lon": "118.299707"}, "name": "黄山国际大酒店", "payMode": [1, 2], "price": 362, "priceStatus": 1, "promotion": [103], "saleType": 1, "score": 4.8, "star": 5, "themes": [3, 2, 4], "totalComments": 133}
{"address": "金城镇 珠山82号", "addressExtend": "", "amenities": null, "brandName": null, "businessZoneList": [{"businessZoneId": 2384, "businessZoneName": "金门机场", "poiType": null}], "cityCode": 1174, "cityName": "泉州", "coverImageUrl": null, "districtName": null, "geoInfo": {"distance": 63229, "name": "市中心", "type": 1, "typeName": "市中心"}, "grade": 2, "hotelId": 763319, "location": {"lat": "24.396442", "lon": "118.314335"}, "name": "金门珠山82号民宿", "payMode": null, "price": null, "priceStatus": 1, "promotion": null, "saleType": 0, "score": null, "star": 0, "themes": [], "totalComments": null}

json转为csv

import csv
import json
import codecs
'''
将json文件格式转为csv文件格式并保存。
'''
class Json_Csv():
	#初始化方法,创建csv文件。
    def __init__(self):
        self.save_csv = open('D:/hotels_out.csv', 'w', encoding='utf-8', newline='')
        self.write_csv = csv.writer(self.save_csv, delimiter=',')  #以,为分隔符
    def trans(self,filename):
        with codecs.open(filename,'r',encoding='utf-8') as f:
            read=f.readlines()
            flag=True
            for index,info in enumerate(read):
                data=json.loads(info)
                if index <3000: #读取json文件的前3000行写入csv文件 。要是想写入全部,则去掉判断。
                    if flag: #截断第一行当做head
                        keys=list(data.keys())  #将得到的keys用列表的形式封装好,才能写入csv
                        self.write_csv.writerow(keys) 
                        flag=False  #释放
                    value=list(data.values())   #写入values,也要是列表形式
                    self.write_csv.writerow(value)
            self.save_csv.close()  #写完就关闭
if __name__=='__main__':
    json_csv=Json_Csv()
    path='D:/hotels.txt'
    json_csv.trans(path)

以上为个人经验,希望能给大家一个参考,也希望大家多多支持脚本之家。 

相关文章

最新评论