栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

Python:从文件夹中读取多个json文件

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Python:从文件夹中读取多个json文件

一种选择是使用os.listdir列出目录中的所有文件,然后仅查找以’.json’结尾的文件:

import os, jsonimport pandas as pdpath_to_json = 'somedir/'json_files = [pos_json for pos_json in os.listdir(path_to_json) if pos_json.endswith('.json')]print(json_files)  # for me this prints ['foo.json']

现在,您可以使用pandas Dataframe.from_dict将json(此时为python字典)读入pandas数据帧:

montreal_json = pd.Dataframe.from_dict(many_jsons[0])print montreal_json['features'][0]['geometry']

印刷品:

{u'type': u'Point', u'coordinates': [-73.6051013, 45.5115944]}

在这种情况下,我将一些json附加到列表中

many_jsons
。我列表中的第一个json实际上是一个geojson,其中包含蒙特利尔的一些地理数据。我已经很熟悉内容了,所以我打印出了“几何图形”,这让我对蒙特利尔感到满意。

以下代码总结了以上所有内容:

import os, jsonimport pandas as pd# this finds our json filespath_to_json = 'json/'json_files = [pos_json for pos_json in os.listdir(path_to_json) if pos_json.endswith('.json')]# here I define my pandas Dataframe with the columns I want to get from the jsonjsons_data = pd.Dataframe(columns=['country', 'city', 'long/lat'])# we need both the json and an index number so use enumerate()for index, js in enumerate(json_files):    with open(os.path.join(path_to_json, js)) as json_file:        json_text = json.load(json_file)        # here you need to know the layout of your json and each json has to have        # the same structure (obviously not the structure I have here)        country = json_text['features'][0]['properties']['country']        city = json_text['features'][0]['properties']['name']        lonlat = json_text['features'][0]['geometry']['coordinates']        # here I push a list of data into a pandas Dataframe at row given by 'index'        jsons_data.loc[index] = [country, city, lonlat]# now that we have the pertinent json data in our Dataframe let's look at itprint(jsons_data)

对我来说,这印:

  countrycity        long/lat0  Canada  Montreal city  [-73.6051013, 45.5115944]1  Canada        Toronto  [-79.3849008, 43.6529206]

了解此代码对于在目录名称“ json”中有两个geojsons可能会有所帮助。每个json具有以下结构:

{"features":[{"properties":{"osm_key":"boundary","extent":[-73.9729016,45.7047897,-73.4734865,45.4100756],"name":"Montreal city","state":"Quebec","osm_id":1634158,"osm_type":"R","osm_value":"administrative","country":"Canada"},"type":"Feature","geometry":{"type":"Point","coordinates":[-73.6051013,45.5115944]}}],"type":"FeatureCollection"}


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/649975.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号