1.數(shù)據(jù)本地化和數(shù)據(jù)持久化 - 通過文件將數(shù)據(jù)儲(chǔ)存到硬盤中
數(shù)據(jù)庫文件:txt、json、plist矛渴、xml、png文件熊杨、mp4曙旭、mp3等
2.文件操作 - 文件內(nèi)容操作
基本步驟:打開文件 --> 操作文件(讀/寫) --> 關(guān)閉文件
1) 打開文件
open(file, mode,encoding = None) - 已指定的方式打開指定文件,并且返回被打開的文件對象
file -- 字符串:需要打開的文件路徑
./ -- 當(dāng)前文件所在目錄
../ -- 當(dāng)前文件所在目錄的上層目錄
mode -- 字符串: 打開方式
r -- 默認(rèn)值晶府,以讀的方式打開文件(不能修改內(nèi)容)桂躏,讀出來的內(nèi)容是文本
w -- 以寫的方式打開文件,覆蓋源文件
a -- 以寫的方式打開文件川陆,追加源文件
rb/br -- 以讀的方式打開剂习,讀出來的內(nèi)容是二進(jìn)制
wb/bw -- 以寫的方式打開,將二進(jìn)制數(shù)據(jù)寫入文件中
+ -- 以讀寫的方式打開文件(了解)
encoding -- 文本編碼方式
utf-8
注意:文本編碼只針對文本文件较沪、二進(jìn)制文件不能設(shè)置編碼方式
2.文件操作
文件對象.read() -- 獲取文件中的內(nèi)容鳞绕,并返回
(讀寫位置默認(rèn)在文件開頭)
文件對象.seek(字節(jié)數(shù)) -- 將讀寫位置移動(dòng)到指定的地方(了解)
def main():
# 1.文件讀操作
f = open('test.txt','r',encoding= 'utf-8')
print(f)
result = f.read()
print(result)
# 2.文件寫操作
"""
文件對象.write(內(nèi)容) -- 將指定內(nèi)容寫到文件里,并覆蓋源文件內(nèi)容
"""
f = open('test.txt','w',encoding= 'utf-8')
wrt = f.write('李白想婆娘')
print(wrt)
# 關(guān)閉文件
"""
文件對象.close()
"""
f.close()
# ======
"""
注意:打開文件的時(shí)候尸曼,如果以讀的方式打開一個(gè)不存在的文件们何,會(huì)出現(xiàn)異常報(bào)錯(cuò)
如果以寫的方式打開一個(gè)不存在的文件,不會(huì)出現(xiàn)異常并且會(huì)創(chuàng)建一個(gè)新的對應(yīng)的文件并打開操作
"""
if __name__ == '__main__':
main()
二進(jìn)制文件操作
rb - 讀的時(shí)候獲取到的是二進(jìn)制數(shù)據(jù)(bytes)
wb - 寫的時(shí)候?qū)懭氲膬?nèi)容要求類型是二進(jìn)制文件
普通的文本文件可以通過二進(jìn)制的形式去打開控轿,影響只是獲取到的內(nèi)容冤竹,和寫的內(nèi)容的數(shù)據(jù)類型拂封;
二進(jìn)制文件只能以二進(jìn)制的形式打開(例如:圖片,音頻)
二進(jìn)制數(shù)據(jù)
一般二進(jìn)制數(shù)據(jù)都是通過網(wǎng)絡(luò)請求獲取到鹦蠕,或者通過讀取本地的二進(jìn)制文件獲取
1)將字符串轉(zhuǎn)換二進(jìn)制
bytes(str,encoding = 'utf-8')
字符串.encode(編碼方式) - str.encode(encoding = 'utf-8')
2) 將二進(jìn)制轉(zhuǎn)換成字符串
str(二進(jìn)制數(shù)據(jù)冒签,encoding = 'utf-8')
二進(jìn)制數(shù)據(jù).decode(編碼方式)
文件上下文
with open(文件路徑,打開方式,編碼方式) as 文件對象:
操作文件
文件操作完成后,會(huì)自動(dòng)關(guān)閉
def main():
with open('./files/test.txt', 'r', encoding='utf-8') as f1:
print(f1.read())
# 1. 普通文本文件以二進(jìn)制的形式打開
f = open('./files/test.txt', 'rb')
reslut = f.read()
print(reslut, type(reslut))
f = open('./files/test.txt', 'wb')
f.write(bytes('bbb', encoding='utf-8'))
# 2. 二進(jìn)制文件
f1 = open('./files/luffy4.jpg', 'rb')
reslut = f1.read()
print(reslut, type(reslut))
f2 = open('./files/aaa.jpg', 'wb')
f2.write(reslut)
if __name__ == '__main__':
main()
1.json數(shù)據(jù)
滿足json格式的數(shù)據(jù)就叫json數(shù)據(jù)
json格式: 一個(gè)json有且只有一個(gè)數(shù)據(jù)钟病,這個(gè)數(shù)據(jù)必須是json支持的數(shù)據(jù)類型
json支持的數(shù)據(jù)類型:
1萧恕、數(shù)字(number) - 包含所有的數(shù)字(整數(shù)和小數(shù)),并且支持科學(xué)計(jì)數(shù)法肠阱。
2票唆、字符串(string) - 通過雙引號(hào)包含的字符集 :"abcd","1234","你好" 字符也可以是轉(zhuǎn)義字符
3、布爾(boolean) - true/false
4辖所、數(shù)組(array) - 相當(dāng)于python中的列表惰说,[100,"abc",true,[1,2,3]]
5缘回、字典(dictionary) - 相當(dāng)于python中的字典吆视,{"a":10,"b":100,"c":true}
6、空值 - null, 相當(dāng)于 None
2.使用json
1) 解析json數(shù)據(jù)(獲取到j(luò)son數(shù)據(jù)后將json中想要的東西解析出來) -- 做前端開發(fā)人的工作
2) 構(gòu)造json數(shù)據(jù)
# =====================1.json轉(zhuǎn)python(json數(shù)據(jù)解析)============
在python中有一個(gè)內(nèi)置庫酥宴,專門負(fù)責(zé)json數(shù)據(jù)的處理: json庫
a. 將json數(shù)據(jù)轉(zhuǎn)換成python數(shù)據(jù)
json數(shù)據(jù) python數(shù)據(jù)
number int/float
string str,可能會(huì)出現(xiàn)將雙引號(hào)變成單引號(hào)
bool bool啦吧,true -> True false -> False
array list
dictionary dict
空 null -> None
json.loads(字符串,encoding = 'utf-8') - 解析json數(shù)據(jù)
字符串要求: 字符串中內(nèi)容本身就是一個(gè)json數(shù)據(jù)(去掉引號(hào)后拙寡,本身就是一個(gè)json數(shù)據(jù))
result = json.loads('"abc"',encoding='utf-8')
print(result,type(result)) # abc <class 'str'>
result = json.loads('100',encoding='utf-8')
print(result,type(result)) # 100 <class 'int'>
result = json.loads('true',encoding='utf-8')
print(result,type(result)) # True <class 'bool'>
result = json.loads('[10,23,"nsq",true]',encoding='utf-8')
print(result,type(result)) # [10, 23, 'nsq', True] <class 'list'>
result = json.loads('{"a": 100,"b": false}',encoding='utf-8')
print(result,type(result)) # {'a': 100, 'b': False} <class 'dict'>
with open('jsontest.json','r',encoding='utf-8')as f:
result = f.read()
result_py = json.loads(result,encoding='utf-8')
result1 = result_py['data']
max_dict = max(result1, key=lambda x: int(x['favourite']))
print(max_dict['name'],max_dict['text'],max_dict['favourite'])
# ======================2.將python 轉(zhuǎn)換為json 數(shù)據(jù)======================
"""
python數(shù)據(jù) json數(shù)據(jù)
int/float number
bool True => true , False => false
str string '' => ""
list,tuple array
dict dictionary
空值 None => null
json.dumps(python數(shù)據(jù)) ==》 將python數(shù)據(jù)轉(zhuǎn)換為對應(yīng)的json數(shù)據(jù) 其結(jié)果是字符串類型
"""
result = json.dumps('abc')
print(result,type(result))
# ===================3.json 文件操作=====================
"""
json.load(文件對象) - 將文件對象中的文件內(nèi)容轉(zhuǎn)換為python數(shù)據(jù)
要求文件內(nèi)容是json數(shù)據(jù)
json.dump(python數(shù)據(jù)授滓,文件對象) - 將python數(shù)據(jù)轉(zhuǎn)換成json數(shù)據(jù),結(jié)果(字符串)再寫入指定文件中
"""
with open('test.txt','r',encoding='utf-8')as f:
result = json.load(f)
print(type(result),result)