Python 官方文档:入门教程 => 点击学习
固定大小 from functools import partial i = 0 f = open('lgtm-monitor.dat', 'rb') f2 = open('f.txt', 'w') records = iter
from functools import partial
i = 0
f = open('lgtm-monitor.dat', 'rb')
f2 = open('f.txt', 'w')
records = iter(partial(f.read, 2), b'') # 每次2字节
for r in records:
j = 0
r_int = int.from_bytes(r, byteorder='big') #将 byte转化为 int
i += 1
print('i={0}:{1}'.fORMat(i, r_int))
f2.write(str(r_int)+' ')
if 8==i:
f2.write('\n')
i = 0
break
f.close()
f2.close()
二进制文件使用 read( size)
with open('lgtm-monitor.dat', 'rb') as fp:
data = fp.read(2) #type(data) === bytes
text = int.from_bytes(data, byteorder='big')
fp.close()
--结束END--
本文标题: python 读取二进制文件
本文链接: https://www.lsjlt.com/news/191278.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
下载Word文档到电脑,方便收藏和打印~
2024-03-01
2024-03-01
2024-03-01
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
0