Python:从文件夹中读取多个json文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/30539679/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Python: Read several json files from a folder
提问by donpresente
I would like to know how to read several json
files from a single folder (without specifying the files names, just that they are json files).
我想知道如何json
从一个文件夹中读取多个文件(不指定文件名,只是它们是 json 文件)。
Also, it is possible to turn them into a pandas
DataFrame?
另外,是否可以将它们转换为pandas
DataFrame?
Can you give me a basic example?
你能给我一个基本的例子吗?
采纳答案by Scott
One option is listing all files in a directory with os.listdirand then finding only those that end in '.json':
一种选择是使用os.listdir列出目录中的所有文件,然后仅查找以“.json”结尾的文件:
import os, json
import pandas as pd
path_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']
Now you can use pandas DataFrame.from_dictto read in the json (a python dictionary at this point) to a pandas dataframe:
现在,您可以使用 pandas DataFrame.from_dict将 json(此时是一个 Python 字典)读入一个Pandas数据帧:
montreal_json = pd.DataFrame.from_dict(many_jsons[0])
print montreal_json['features'][0]['geometry']
Prints:
印刷:
{u'type': u'Point', u'coordinates': [-73.6051013, 45.5115944]}
In this case I had appended some jsons to a list many_jsons
. The first json in my list is actually a geojsonwith some geo data on Montreal. I'm familiar with the content already so I print out the 'geometry' which gives me the lon/lat of Montreal.
在这种情况下,我已将一些 jsons 附加到 list many_jsons
。我列表中的第一个 json 实际上是一个带有一些蒙特利尔地理数据的geojson。我已经熟悉内容,所以我打印了“几何”,它给了我蒙特利尔的经纬度。
The following code sums up everything above:
以下代码总结了以上所有内容:
import os, json
import pandas as pd
# this finds our json files
path_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 json
jsons_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 it
print(jsons_data)
for me this prints:
对我来说这打印:
country city long/lat
0 Canada Montreal city [-73.6051013, 45.5115944]
1 Canada Toronto [-79.3849008, 43.6529206]
It may be helpful to know that for this code I had two geojsons in a directory name 'json'. Each json had the following structure:
知道对于此代码,我在目录名称“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"}
回答by Saravana Kumar
To read the json files,
要读取 json 文件,
import os
import glob
contents = []
json_dir_name = '/path/to/json/dir'
json_pattern = os.path.join(json_dir_name, '*.json')
file_list = glob.glob(json_pattern)
for file in file_list:
contents.append(read(file))
回答by Ami Tavory
回答by Sma Ma
Loads all files that end with * .json from a specific directory into a dict:
将特定目录中以 * .json 结尾的所有文件加载到字典中:
import os,json
path_to_json = '/lala/'
for file_name in [file for file in os.listdir(path_to_json) if file.endswith('.json')]:
with open(path_to_json + file_name) as json_file:
data = json.load(json_file)
print(data)
Try it yourself: https://repl.it/@SmaMa/loadjsonfilesfromfolderintodict
自己试试:https: //repl.it/@SmaMa/loadjsonfilesfromfolderintodict