日本免费高清视频-国产福利视频导航-黄色在线播放国产-天天操天天操天天操天天操|www.shdianci.com

學無先后,達者為師

網站首頁 Python教程 正文

Python如何存儲和讀取ASCII碼形式的byte數據_python

作者:rysander ? 更新時間: 2022-07-17 Python教程

存儲和讀取ASCII碼形式的byte數據

Python可以存byte數據到txt,但不要用str的方式直接存,轉成數字列表儲存,這樣方便讀取

L = []
a = b'\x00\xef\xa2\xa0\xb3\x8b\x9d\x1e\xf8\x98\x19\x39\xd9\x9d\xfdthe first line\n\r\a\b\t\\\f\'\"\v\b\n\000'
print(a)
for each in a:
    L.append(int(each))
with open('data.txt','w') as p:
    p.write(str(L))
print(L)
>>> [0, 239, 162, 160, 179, 139, 157, 30, 248, 152, 25, 57, 217, 157, 253, 116, 104, 101, 32, 102, 105, 114, 115, 116, 32, 108, 105, 110, 101, 10, 13, 7, 8, 9, 92, 12, 39, 34, 11, 8, 10, 0]
 
 
with open('data.txt','r') as p:
    line = p.readline()
print(b''.join([bytes([int(i)]) for i in line[1:-1].split(',')]))
>>> b'\x00\xef\xa2\xa0\xb3\x8b\x9d\x1e\xf8\x98\x199\xd9\x9d\xfdthe first line\n\r\x07\x08\t\\\x0c\'"\x0b\x08\n\x00'

Python ASCII碼的獲取

ord函數可以獲取字符的ASCII碼,用法如下:

代碼實現:

#ord(‘字符')可以返回該字符的ASCII碼
print(ord('a'))

運行結果:

在這里插入圖片描述

原文鏈接:https://blog.csdn.net/weixin_40222586/article/details/102948919

欄目分類
最近更新