json數(shù)據(jù)
1.什么是json數(shù)據(jù)
json是一種數(shù)據(jù)格式,滿足json格式的數(shù)據(jù)就是json數(shù)據(jù)。
文件后綴是.json,并且文件中內(nèi)容滿足json格式
2.json格式
一個json中只有一個數(shù)據(jù);并且這個數(shù)據(jù)是json支持的數(shù)據(jù)類型的數(shù)據(jù)
json支持的數(shù)據(jù)類型
數(shù)字類型 - 包含所有的數(shù)字,包括整數(shù)和小數(shù), 例如: 100, 12.5, -20
字符串 - 使用雙引號括起來字符集, 例如: "123", "abc123", "&*ash"
布爾 - true和false
數(shù)組 - 相當于python中的列表, 使用中括號括起來,括號里面是json支持的任意類型的數(shù)據(jù)
例如:["abc", 100, true], [12, 89, 89, 90]
字典 - 相當于python中的字典, 使用{}括起來,括號里面是鍵值對。
鍵一般是字符串,值是json支持的任意類型的數(shù)據(jù)
{"name": "張三", "age": 18}
特殊值 - null(相當于None), 表示空
3.python中有一個內(nèi)置的模塊用來支持對json數(shù)據(jù)的處理: json
a.將json數(shù)據(jù)轉(zhuǎn)換成python數(shù)據(jù)
b.將python數(shù)據(jù)轉(zhuǎn)換成json數(shù)據(jù)
.將json數(shù)據(jù)轉(zhuǎn)換成python數(shù)據(jù)
loads(字符串) - 將json格式的數(shù)據(jù)轉(zhuǎn)換python對應的數(shù)據(jù)
注意:這兒的字符串的內(nèi)容必須是json格式的數(shù)據(jù)
json python
數(shù)字 整型/浮點型
字符串 字符串(雙引號會變單引號)
布爾 布爾(true -> True, false -> False)
數(shù)組 列表
字典 字典
null None
數(shù)字轉(zhuǎn)int/float
py1 = json.loads('100')
print(py1, type(py1))
py2 = json.loads('100.12')
print(py2, type(py2))
b. 字符串
py3 = json.loads('"json"')
print(py3, type(py3))
c. 布爾
py4 = json.loads('true')
print(py4)
d.列表
py5 = json.loads('[100, "abc", true, null]')
print(py5)
e.字典
py6 = json.loads('{"a": 1, "b":[1, 2], "c": true}')
print(py6)
將python數(shù)據(jù)轉(zhuǎn)換成json數(shù)據(jù)
dumps(數(shù)據(jù)) - 將python數(shù)據(jù)轉(zhuǎn)換成內(nèi)容符合json格式的字符串
注意:最終結(jié)果是字符串
python json
int/float 數(shù)字
字符串 字符串(單引號會變雙引號)
布爾 布爾(True - true, False - false)
列表/元祖 數(shù)組
字典 字典
a. int和float
js1 = json.dumps(100)
print(js1, type(js1))
js1 = json.dumps(100.12)
print(js1, type(js1))
b.字符串
js2 = json.dumps('hello world')
print(js2)
c.布爾
js3 = json.dumps(True)
print(js3)
d.列表、元祖
js4 = json.dumps((10, 'abc', True))
print(js4)
js4 = json.dumps([100, 'aaa', False, None])
print(js4)
e.字典
js5 = json.dumps({'a': 10, 'b': 'abc', 'c': ['a', 'b']})
print(js5)
1.什么是異常
程序執(zhí)行過程中出現(xiàn)錯誤,也叫出現(xiàn)異常
2.異常捕獲
讓本來會出現(xiàn)異常的位置,不出現(xiàn)異常,而是自己去處理異常出現(xiàn)的情況
3.怎么捕獲異常
情況一:捕獲所有的異常
a.語法:
try:
代碼段1
except:
代碼段2
b.執(zhí)行過程: 執(zhí)行代碼段1,如果代碼段1中出現(xiàn)異常,不會奔潰,而是馬上執(zhí)行代碼段2。
如果代碼段1沒有異常,不會執(zhí)行代碼段2
try:
print(int('abc'))
print('~~~~~')
print([1, 2, 3][10])
print('++++++')
except:
print('出現(xiàn)異常!')
print('===========')
情況二:捕獲指定的異常
a.語法:
try:
代碼段1
except 錯誤類型名:
代碼段2
b.執(zhí)行過程: 執(zhí)行代碼段1,當代碼段1出現(xiàn)指定類型的異常后不奔潰,而是執(zhí)行代碼段2
try:
print([1, 2, 3][10])
print('~~~~~')
print(int('abc'))
print('++++++')
except IndexError:
print('下標越界!')
print('!!!!!')
情況三:同時捕獲多個異常,對不同的異常做出相同的反應
try:
代碼段1
except (錯誤類型1, 錯誤類型2, 錯誤類型3...):
代碼段2
執(zhí)行過程:執(zhí)行代碼段1,當代碼段1中出現(xiàn)了指定的異常,不崩潰,然后執(zhí)行代碼2
try:
print([1, 2][10]) # IndexError
# print(int('abc')) # ValueError
# print({'a': 1}['b']) # KeyError
except (IndexError, KeyError, ValueError, FileNotFoundError, StopIteration):
print('出現(xiàn)多種異常中的一個!')
情況四:同時捕獲多個異常,對不同的異常做出不同的反應
try:
代碼段1
except 錯誤類型1:
代碼段2
except 錯誤類型2:
代碼段3
try:
# print([1, 2][10])
print(int('abc'))
except KeyError:
print('鍵錯誤!')
except ValueError:
print('值錯誤!')
except IndexError:
print('下標越界!!!!')
try-except-finally
try:
代碼段1
except:
代碼段2
finally:
代碼段3
不管代碼段1中是否出現(xiàn)異常,也不管異常是否能夠捕獲到,
finally后面的代碼段3都會執(zhí)行!
try:
print([1, 2][10])
except IndexError:
print('下標越界錯誤!')
finally:
print('最后?。?!')
print('最后~~~')
練習:輸入成績,直到輸入的數(shù)據(jù)輸入正確為止!
while True:
try:
score = float(input('輸入成績:'))
break
except ValueError:
print('輸入有誤!請輸入數(shù)字')
# score = float(input('輸入成績:'))
封裝一個函數(shù),功能是獲取指定文件中的內(nèi)容(普通文本文件)
從封裝角度:調(diào)用者做的事情越少越好!
def get_file_content(file):
try:
with open(file, 'r', encoding='utf-8') as f:
content = f.read()
return content
except FileNotFoundError:
print('文件路徑有誤!')
return ''
print(get_file_content('new.json2'))
拋出異常:主動讓程序出現(xiàn)異常
語法:
raise 錯誤類型 - 程序執(zhí)行到raise的時候直接拋出異常
注意:錯誤類型必須是一個類,并且是Exception的子類