1.整理非結構性資料
2.整理成半結構性資料json資料 (可以使用json 線上編輯器來測試自己的json檔案有無錯誤)
3.整理出結構性資料(讀出自己所需的資料)
1.整理非結構性資料
id | 1000 |
type | BigBox |
name | Mall of America |
address | 340 W Market |
city | Bloomington |
state | MN |
zip | 55425 |
location | |
lat=44.85466 | |
lon=93.24565 | |
hours | Mon: 10-9:30; Tue: 10-9:30; Wed: 10-9:30; Thurs: 10-9:30; Fri: 10-9:30; Sat: 10-9:30; Sun: 11-7 |
services | |
Geek Squad Services, | |
Best Buy Mobile, | |
Best Buy For Business | |
id | 3000 |
type | BigBox2 |
name | Mall of America2 |
address | 340 W Market2 |
city | Bloomingto2n |
state | MN2 |
zip | 554251 |
location | |
lat=44.854662 | |
lon=93.245652 | |
hours | Mon: 10-9:320; Tue: 10-9:30; Wed: 10-9:30; Thurs: 10-9:30; Fri: 10-9:30; Sat: 10-9:30; Sun: 11-7 |
services | |
Geek Squad Services2, | |
Best Buy Mobile2, | |
Best Buy For Busines2s | |
在組json 資料時注意:
[ ] =只有值的時候 ["值1","值2","值3","值n"]
{ } = 有 key : value 時 使用
2.整理成半結構性資料json資料
(可以使用json 線上編輯器來測試自己的json檔案有無錯誤)
json14.json
[
{
"id": 1000,
"type": "BigBox",
"name": "Mall of America",
"address": "340 W Market",
"address2": "",
"city": "Bloomington",
"state": "MN",
"zip": "55425",
"location": {
"lat": 44.85466,
"lon": -93.24565
},
"hours": "Mon: 10-9:30; Tue: 10-9:30; Wed: 10-9:30; Thurs: 10-9:30; Fri: 10-9:30; Sat: 10-9:30; Sun: 11-7",
"services": [
"Geek Squad Services",
"Best Buy Mobile",
"Best Buy For Business"
]
},
{
"id": 1002,
"type": "BigBox",
"name": "Tempe Marketplace",
"address": "1900 E Rio Salado Pkwy",
"address2": "",
"city": "Tempe",
"state": "AZ",
"zip": "85281",
"location": {
"lat": 33.430729,
"lon": -111.89966
},
"hours": "Mon: 10-9; Tue: 10-9; Wed: 10-9; Thurs: 10-9; Fri: 10-10; Sat: 10-10; Sun: 10-8",
"services": [
"Windows Store",
"Geek Squad Services",
"Best Buy Mobile",
"Best Buy For Business"
]
}
]
複習:新增陣列與物件
store_list = []#可存list陣列 [第一筆,第二筆]
store_list.append("新竹")
store_list.append("台中")
print(store_list)#['新竹', '台中']
store_details = {} #宣告字典類型 (可存key:value)
store_details['name'] = "John"
store_details['city'] = "台北"
print(store_details)#{'name': 'John', 'city': '台北'}
3.再開一個檔案去讀取json14.json,整理出結構性資料(讀出自己所需的資料)
從大型數據集中解析出json(字典)。key:values從每個對像中提取一些內容,印出結果。
這裡只需要所有的name跟city 的欄位,請把整理抓出後存入陣列、並印出結果
例:
json15.py
""""
從大型數據集中解析出數組。
key:values從每個對像中提取一些內容,
印出結果。
[{key:value,key:value},{key:value,key:value}]
"""
import json
input_file = open ('json14.json')
json_array = json.load(input_file)
store_list = []#可存list陣列 [第一筆,第二筆]
for item in json_array:
store_details = {} #宣告字典類型 (可存key:value)
store_details['name'] = item['name']
store_details['city'] = item['city']
store_list.append(store_details)
print(store_list)
4.這次只需要id、location中的lon(經度的)資料,請把整理抓出後存入陣列、並印出結果
例:
json16.json
import json
input_file = open ('json14.json')
json_array = json.load(input_file)
store_list = []#可存list陣列 [第一筆,第二筆]
for item in json_array:
store_details = {}#宣告字典類型 (可存key:value)
store_details['id'] = item['id']
store_details['location'] = item['location']['lon'] #抓出經度longitude
store_list.append(store_details)
print(store_list)
Yiru@Studio - 關於我 - 意如