我想用python写入二进制的文件,例如字符串"abcd"写为6162 6364(十六进制),我用struct模块存储后发现struct只是把字符串转换为字节,实际写入的内容还是字符串,不是二进制序列。
我打开的方式是二进制写
怎么才会写入二进制序列(0101)呢?
相关问题
- how to define constructor for Python's new Nam
- streaming md5sum of contents of a large remote tar
- How to get the background from multiple images by
- Evil ctypes hack in python
- Correctly parse PDF paragraphs with Python
我网上找了个例子,在python 3.7.4中运行是可以的:
import struct
a_list = [33,1,1,1,4]
with open("d:\a.bin","wb") as fp:
for x in a_list:
s = struct.pack('b',x)
fp.write(s)
fp.close()
print("done")
在ultraedit下,用16进制看。结果是对的