binascii.hexlify(data)
Return the hexadecimal representation of the binary data. Every byte of data is converted into the corresponding 2-digit hex representation. The resulting string is therefore twice as long as the length of data.
binascii.a2b_hex(hexstr)
binascii.unhexlify(hexstr)
Return the binary data represented by the hexadecimal string hexstr. This function is the inverse of b2a_hex(). hexstr must contain an even number of hexadecimal digits (which can be upper or lower case), otherwise a TypeError is raised.
实际项目中经常遇到读取二进制问题,Python下读取二进制文件一般用Python的文件打开读写相关函数和struct.
1。获取文件名filename,可用对话框,也可以直接手写
2。打开文件,这里中是打开,像看书一样,只是打开书,还没有看里面的内容,可以看,也可以不看
binFile=open(filename,'rb')
3。那就看看吧,binFile现在就是书的名字了。这里我们读入4个字,如果read()将读入整个文件
context=binFile.read(4)
4。读入之后问题来了,context包含四个字(4Bytes),但这四个字是加密的(二进制)。这时要找翻译struct.unpack
realContext=struct.unpack(format,context)
5。format的就是加密规则了,由1,2,4,8个字节组成其他类型,或者其他类型的数组。
a,b,c,d=struct.unpack('4c',context)
读入四个字符
对应格式参考 www.jianshu.com/p/5a985f29fa81
6。打开文件后用完一定要关闭,和看书一个样,看完后要合上,不合上后果自己去想
binFile.close()
7。查找内容,文件内容可以一个一个字的按顺序读出,但如果已经知道内容在什么位置,可以用查找方式读出。
binFile.seek(100)
context=binFile.read(4)
读出100位置后面的4个字,(101,102,103,104)
8。Python还提供也一些高级读写功能,如整行读取等
图片,视频都是二进制的,你读取的时候写 rb ,然后你在请求的时候记得把视频转成.content
像这样load_src = requests.get("http://"+srcs[i], headers=headers).content