3 27Day06字元編碼複習,檔案處理模式

2022-08-11 04:03:20 字數 4565 閱讀 4114

上節課複習

二進位制字串:用來傳輸的字串

b''2、二進位制字串按位元組為最小儲存單位存放資料

3、所有資料轉化為二進位制字串都應該用utf-8進行編碼嗎???

--只是文字資料用utf-8編碼

字串比較大小的前提依據就是ascii碼表

# 字元與ascii之間的轉化

res= ord('a')

print(res)

run ==》

65res = chr('65')

print(res)

run ==》

'a'ascii:dbcs 雙位元組儲存可以存放中文的一些文字與字元,可以完成字元與整數(asicc表中整數)的轉化

print(ord('⑩'),chr('9323'))

run ==》

9321 ⑪

一、檔案處理

1、r w 模式結合

# w : 沒有檔案新建檔案,有檔案就清空檔案

w = open ('1.txt','w',encoding='utf-8')

w.write('123')

w.flush() # 在寫入大量資料時要即時處理記憶體空間,不然記憶體可能溢位導致資料丟失

w.write('456')

w.flush() # 最後一次flush操作可以省略

# 1.將村內的資料重新整理到硬碟中; 2. 釋放檔案資源

w.close

文字型別的檔案複製:先讀再寫

r = open('1.txt','r',encoding'utf-8')

w = open('11.txt','w',encoding'utf-8')

for line in r: # 遍歷就是一行一行讀取讀檔案的流

w.write(line)

w.close

r.close

# with open

# 將檔案的關閉交給with管理,當with中邏輯結束後,系統會自動釋放檔案

with open() as f:

with open('1.txt', 'r', endoding='utf-8') as r, open('111.txt', 'w', endoding='utf-8') as 'w':

for line in r:

w.write(line)

w.flush()

w.close # 系統自動操作

r.close # 系統自動操作

2、檔案操作模式

r:讀w:寫(無建立,有清空)

a:追加(有建立的功能)

t:按字元操作

b:非文字檔案必須採用二進位制模式來處理

+:可讀可寫

r+:不會建立檔案的可讀可寫 | w+:建立清空檔案的可讀可寫 | a+:建立並且不清空檔案

r+:二進位制讀 | wb:建立清空檔案的二進位制寫 | ab:建立不清空檔案(追加)的二進位制寫

rb+ | wb+ | ab+

x:寫模式;建立檔案,如果檔案已存在,就報錯

u:# 借助讀寫,完成追加 (麻煩)

with open('1.txt', 'rt', encoding='utf-8') as f1:

data = f1.read()

data += '666'

with open('1.txt', 'wt', encoding='utf-8') as f2:

f2.write(data)

# a為寫模式,在資料後追加寫入新資料

with open('1.txt', 'a', encoding='utf-8') as f:

f.write('777')

3、with完成文字檔案的複製

with open('source.txt', 'r', encoding='utf-8') as f1, \

open('target.txt', 'a', encoding='utf-8') as f2:

# 讀取乙個位元組,如果是行/檔案等結束標識,就返回該標識,否則返回none

print(f1.newlines)

first_data = f1.read(9)

f2.write(first_data)

f2.flush()

print(f1.newlines)

second_data = f1.readline()

f2.write(second_data)

f2.flush()

last_data = f1.read()

f2.write(last_data)

f2.flush()

print(f1.newlines)

data = f1.read()

print(data)

# 邊讀邊寫複製

with open('f:\**文件\檔案\day8\邊讀邊寫複製檔案.txt', 'r+', encoding='utf-8') as f1:

with open('f:\**文件\檔案\day8\邊讀邊寫複製檔案1.txt', 'a+', encoding='utf-8') as f2:

for i in f1:

f2.write(i)

f2.flush()

4、非文字檔案複製

從模式採用 b 模式,不需要關心編碼問題

with open('f:\**文件\檔案\day8\\測試.png', 'rb') as r, \

open('f:\**文件\檔案\day8\\測試1.png', 'wb') as w:

data_r = r.read()

w.write(data_r)

5、游標:必須在 b 模式下操作,seek中偏移的是位元組

5.1 游標的相關方法

方法: seek (偏移量,偏移位置)

偏移量:移動的位元組數

偏移位置:

0 :從檔案開始位置開始偏移 | 1:從當前游標位置開始偏移 | 2:從檔案末尾開始偏移

5.2 游標相關的讀寫操作

# 游標讀

with open('source.txt', 'rb') as f:

d1 = f.read(11)

print(d1)

# print(d1.decode('utf-8'))

# 當前游標的位置

print(f.tell())

# 游標操作 - 從末尾位置開始

f.seek(-3, 2)

d2 = f.read()

print(d2.decode('utf-8')) # 890

# # 游標操作 - 從當前位置開始

# f.seek(-3, 1)

# d2 = f.read()

# print(d2.decode('utf-8')) # 34567890

# # 游標操作 - 從頭開始

# f.seek(3, 0)

# d2 = f.read()

# # print(d2)

# print(d2.decode('utf-8')) # 好1234567890

# 游標寫:會覆蓋書寫

with open('source.txt', 'rb+') as f:

f.seek(11)

# print(f.read())

f.write(b'000')

5.3 根據游標在大檔案中取出多個指定位置的部分位元組內容

tagdata = b''

with open('001.png', 'rb') as f:

# 通過其他途徑(sys模組)來獲取檔案總大小

data = f.read()

length = len(data)

# 開頭

f.seek(0, 0)

d1 = f.read(10)

# 1/3

f.seek(length // 3, 0)

d2 = f.read(10)

# 2/3

f.seek(length // 3 * 2, 0)

d3 = f.read(10)

# 末尾

f.seek(-10, 2)

d4 = f.read(10)

tagdata = d1 + d2 + d3 + d4

# 秒傳依據

print(tagdata)

newdata = b""

with open('001.png', 'rb') as f:

data = f.read()

length = len(data)

f.seek(0, 0)

newdata += f.read(10)

f.seek(length // 3, 0)

newdata += f.read(10)

f.seek(length // 3 * 2, 0)

newdata += f.read(10)

f.seek(-10, 2)

newdata += f.read(10)

if newdata == tagdata:

print('秒傳成功')

else:

print('慢慢傳去吧')

day10 字元編碼

這節理論比較多,要好好理解,如果理解不了,一定要把結論記下來 所以基本沒寫別的內容 ascii表 1 只支援英文本串 2 採用8位二進位制數對應乙個英文本串 gbk表 1 支援英文本元 中文字元 2 採用8位 8bit 1bytes 二進位制數對應乙個英文本串 採用16位 16bit 2bytes ...

Day08字元編碼

day08 知識儲備 硬碟 由硬碟載入到記憶體,cpu從記憶體中取 軟體產生的資料都是先儲存在記憶體中 檔案,輸入文字,儲存到記憶體,記憶體是硬體,硬體只能儲存2進製,所以需要轉換 文字編輯器,輸入文字的時候,是乙個轉成二進位制儲存到記憶體,然後反解成文字顯示到螢幕的過程 字元編碼 將字元轉換 編碼...

day08字元編碼問題

1.把字元讀入python直譯器 python直譯器相當於文字編輯器 字元編碼 2.識別字元 3.往終端列印結果 字元編碼 第一步解決方法 python2預設使用ascill碼讀入字元 python3預設使用utf8 可以使用coding gbk 修改為 gbk讀入字元 第二步解決方法 python...