網站首頁 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
相關推薦
- 2022-07-14 React父子組件傳值(組件通信)的實現方法_React
- 2023-02-10 Python中如何自定義函數_python
- 2022-07-17 SQL?Server中鎖的用法_MsSql
- 2022-09-13 Oracle使用fy_recover_data恢復truncate刪除的數據_oracle
- 2022-11-10 Redis源碼設計剖析之事件處理示例詳解_Redis
- 2022-06-13 ASP.NET?Core中的靜態文件介紹_實用技巧
- 2022-08-03 GoFrame?gtree樹形結構的使用技巧示例_Golang
- 2022-04-16 Python3如何將源目錄中的圖片用MD5命名并可以設定目標目錄_python
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支