python高階寶典14 json 資料處理

2021-08-20 02:48:55 字數 1346 閱讀 4854

先看**:

## 用 loads() 讀取json,返回乙個python字典

import json

stringjson = '' # json字串總是用雙引號

jsontopython = json.loads(stringjson)

print(jsontopython) #

# dumps() 輸出 json 格式

pythonvalue =

stringjson = json.dumps(pythonvalue)

print(stringjson)

# 1. 命令列讀取請求天氣的位置 -- 處理 sys.argv

# 3. 講json資料轉成python資料結構 -- json.loads()

# 4. 列印未來兩天的天氣

# prints the weather for a location from the command line.

import json, requests, sys

# compute location from command ine arguments.

if len(sys.argv) < 2:

print('usage: quickweather.py location')

sys.exit()

location = ' '.join(sys.argv[1:])

# download the json data from openweathermap.org's api.

url = '

&mcode=1c:6b:42:33:e8:a6:dc:a2:11:6e:26:ec:84:bd:42:e3:8e:6b:57:9a;com.example.administrator.jsontest' % (location)

response = requests.get(url)

response.raise_for_status()

# load json data into a python variable.

weatherdata = json.loads(response.text)

print('current weather in %s:' % (location) )

print(weatherdata)

# w = weatherdata['list'] # 根據具體資料列印

# print(w[0]['weather'][0]['main']) # 根據具體資料列印

另外,國家氣象局的可以試一下,好像註冊後要等48小時審核。

JS筆記 14 JSON格式處理器

json字串 json處理器有兩個靜態方法 json.stringify json還有第三個引數,也不常用到,感興趣的朋友可參考其他文件 console.log json.stringify 123 123 console.log json.stringify 123 123 console.log...

14 Python基礎 高階變數

1.與列表類似,但不同在於元組的元素型別可以不同,且不能修改 保證資料安全 2.用於儲存一串 資訊,資料之間使用 分隔 3.元組用 定義 4.元組的索引 從 0開始 5.索引就是資料在元組中位置編號 建立元組 info tuple 小明 5,10 info tuple xiaoming 18,10 ...

Python高階學習筆記之JSON

使用json庫 json 是一種輕量級的文字資料交換格式。與 xml 相比,擁有更小 更快 更易解析的特點。其結構和 python 的list dict有點相似。資料表示 json 中資料都以名稱 值的形式表示,名稱包括在一對雙引號 中,值則有多種形式,多條資料之間用逗號,隔開。這種表示方式與 py...