python中struct.unpack的用法
4/25/2009 12:18:21 PM
Python中按一定的格式取出某字符串中的子字符串,使用struck.unpack是非常高效的。
# 取前5個字符, 跳過4個字符華, 再取3個字符 format = '5s 4x 3s'
2. 使用struck.unpack獲取子字符串
import struct print struct.unpack(format, 'Test astring') #('Test', 'ing')
import struct theString = He is not very happy ' format = '2s 1x 2s 5x 4s 1x 5s ' print ' '.join(struct.unpack(format, theString))
輸出結果:
利用unpack(),讀入一個bin文件,rawstring是一個str型的字串: rawfile = open("lcd.raw","rb")
|
在此處將rawstring轉成Byte型數據得到一個rawdata的元組進行處理。
Format C Type Python Notes
x | pad byte | no value | |
c | char | string of length 1 | |
b | signed char | integer | |
B | unsigned char | integer | |
h | short | integer | |
H | unsigned short | integer | |
i | int | integer | |
I | unsigned int | long | |
l | long | integer | |
L | unsigned long | long | |
q | long long | long | (1) |
Q | unsigned long long | long | (1) |
f | float | float | |
d | double | float | |
s | char[] | string | |
p | char[] | string | |
P | void * | integer |
本文為張軍原創文章,轉載無需和我聯系,但請注明來自張軍的軍軍小站,個人博客http://m.eyofj.com
更多文章、技術交流、商務合作、聯系博主
微信掃碼或搜索:z360901061

微信掃一掃加我為好友
QQ號聯系: 360901061
您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對您有幫助就好】元
