python讀寫二進(jìn)制文件的方法
本文實(shí)例講述了python讀寫二進(jìn)制文件的方法。分享給大家供大家參考。具體如下:
初學(xué)python,現(xiàn)在要讀一個(gè)二進(jìn)制文件,查找doc只發(fā)現(xiàn) file提供了一個(gè)read和write函數(shù),而且讀寫的都是字符串,如果只是讀寫char等一個(gè)字節(jié)的還行,要想讀寫如int,double等多字節(jié)數(shù) 據(jù)就不方便了。在網(wǎng)上查到一篇貼子,使用struct模塊里面的pack和unpack函數(shù)進(jìn)行讀寫。下面就自己寫代碼驗(yàn)證一下。
>>> from struct import * >>> file = open(r"c:/debug.txt", "wb") >>> file.write(pack("idh", 12345, 67.89, 15)) >>> file.close()
接著再將其讀進(jìn)來(lái)
>>> file = open(r"c:/debug.txt", "rb") >>> (a,b,c) = unpack("idh",file.read(8+8+2)) >>> a,b,c (12345, 67.890000000000001, 15) >>> print a,b,c 12345 67.89 15 >>> file.close()
在操作過(guò)程中需要注意數(shù)據(jù)的size
注意 wb,rb中的b字,一定不可以少
方法1:
myfile=open('c:\\t','rb') s=myfile.read(1) byte=ord(s) #將一個(gè)字節(jié) 讀成一個(gè)數(shù) print hex(byte) #轉(zhuǎn)換成16進(jìn)制的字符串
方法2
import struct myfile=open('c:\\t','rb').read(1) print struct.unpack('c',myfile) print struct.unpack('b',myfile)
寫入
To open a file for binary writing is easy, it is the same way you do for reading, just change the mode into “wb”.
file = open("test.bin","wb")
But, how to write the binary byte into the file?
You may write it straight away with hex code like this:
file.write("\x5F\x9D\x3E") file.close()
Now, check it out with hexedit,
hexedit test.bin
You will see this:
00000000 5F 9D 3E _.> 00000020 00000040
Now, open the file to append more bytes:
file = open("test.bin","ab")
What if I want to store by bin value into a stream and write it one short?
s ="\x45\xF3" s = s + "%c%c" % (0x45,0xF3) file.write(s) file.close()
Any convenient ways if I can obtained a hex string, and want to convert it back to binary format?
Yes, you just need to import binascii
import binascii hs="5B7F888489FEDA" hb=binascii.a2b_hex(hs) file.write(hb) file.close()
希望本文所述對(duì)大家的Python程序設(shè)計(jì)有所幫助。
相關(guān)文章
Python按照l(shuí)ist dict key進(jìn)行排序過(guò)程解析
這篇文章主要介紹了Python按照l(shuí)ist dict key進(jìn)行排序過(guò)程解析,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-04-04Python利用itchat模塊定時(shí)給朋友發(fā)送微信信息
這篇文章主要介紹了在Python中利用itchat模塊編寫一個(gè)爬蟲腳本,可以實(shí)現(xiàn)每天定時(shí)給朋友發(fā)微信暖心話,感興趣的可以跟隨小編一起學(xué)習(xí)一下2022-01-01在Python中實(shí)現(xiàn)shuffle給列表洗牌
今天小編就為大家分享一篇在Python中實(shí)現(xiàn)shuffle給列表洗牌,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-11-11python使用magic模塊進(jìn)行文件類型識(shí)別方法
今天小編就為大家分享一篇python使用magic模塊進(jìn)行文件類型識(shí)別方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-12-12Python 測(cè)試框架unittest和pytest的優(yōu)劣
這篇文章主要介紹了Python 測(cè)試框架unittest和pytest的優(yōu)劣,幫助大家更好的進(jìn)行python程序的測(cè)試,感興趣的朋友可以了解下2020-09-09Python?數(shù)據(jù)類型中的字符串和數(shù)字
這篇文章主要介紹了Python?數(shù)據(jù)類型中的字符串和數(shù)字,Python3中有六個(gè)標(biāo)準(zhǔn)的數(shù)據(jù)類型,Number、String、List、Tuple、Set、Dictionary,加先來(lái)我們就來(lái)看看這幾種數(shù)據(jù)類型的具體相關(guān)介紹,需要的小伙伴可以參考一下2022-02-02深入解析Python中的descriptor描述器的作用及用法
在Python中描述器也被稱為描述符,描述器能夠?qū)崿F(xiàn)對(duì)對(duì)象屬性的訪問(wèn)控制,下面我們就來(lái)深入解析Python中的descriptor描述器的作用及用法2016-06-06pycharm激活碼2020最新分享適用pycharm2020最新版親測(cè)可用
這篇文章主要介紹了pycharm激活碼2020最新分享適用pycharm2020最新版親測(cè)可用,同時(shí)也支持Intellij IDEA激活碼,PHPStorm激活碼大家可以放心使用需要的朋友可以參考下2020-11-11