Jedną z opcji jest wykaz wszystkich plików w katalogu z os.listdir a następnie znalezienie tylko tych, które kończą się „.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']
Teraz można korzystać z pandy DataFrame.from_dict czytać w json (słownikiem python w tym momencie) do dataframe pandy:
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]}
W tym przypadku dołączyłem kilka jsons do listy many_jsons
. Pierwszy json na mojej liście to w rzeczywistości geojson z niektórymi danymi geograficznymi na temat Montrealu. Znam już treść, więc wydrukuję "geometrię", która daje mi lon/lat Montreal.
Poniższy kod podsumowuje wszystko powyżej:
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)
dla mnie to drukuje:
country city long/lat
0 Canada Montreal city [-73.6051013, 45.5115944]
1 Canada Toronto [-79.3849008, 43.6529206]
pomocne może okazać się, że dla tego kodu miałem dwa geojsons w nazwie katalogu ' json ". Każdy json miał następującą strukturę:
{"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"}
Naprawdę pomocny. Zamiast druku, moim pomysłem było zapisanie ich wszystkich w jednej ramce danych pandy, czy powinien to być poprawny kod? utworzyć pustą ramkę danych i zacząć dodawać do niej wiersze? Dzięki @Scott za tę szczegółową odpowiedź! – donpresente
@donpresente Dobre pytanie. Opublikuję edycję, która zajmie się tym, jak uzyskać pożądane dane z json, a następnie przesłać te dane do pandy DataFrame, rząd po rzędzie. – Scott
@donpresente czy kod poniżej ** EDIT ** Ci pomógł? – Scott