python中struct.unpack的用法
4/25/2009 12:18:21 PM
Python中按一定的格式取出某字符串中的子字符串,使用struck.unpack是非常高效的。
# 取前5個(gè)字符, 跳過4個(gè)字符華, 再取3個(gè)字符 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))
輸出結(jié)果:
利用unpack(),讀入一個(gè)bin文件,rawstring是一個(gè)str型的字串: rawfile = open("lcd.raw","rb")
|
在此處將rawstring轉(zhuǎn)成Byte型數(shù)據(jù)得到一個(gè)rawdata的元組進(jìn)行處理。
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 |
本文為張軍原創(chuàng)文章,轉(zhuǎn)載無需和我聯(lián)系,但請(qǐng)注明來自張軍的軍軍小站,個(gè)人博客http://www.dlhighland.cn
更多文章、技術(shù)交流、商務(wù)合作、聯(lián)系博主
微信掃碼或搜索:z360901061

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