Pythonstruct.unpack
來源:懂視網
責編:小采
時間:2020-11-27 14:29:24
Pythonstruct.unpack
Pythonstruct.unpack:1. 設置fomat格式,如下: 代碼如下:# 取前5個字符,跳過4個字符華,再取3個字符 format = '5s 4x 3s' 2. 使用struck.unpack獲取子字符串 代碼如下:import struct print struct.unpack(format, 'Test astring') #('
導讀Pythonstruct.unpack:1. 設置fomat格式,如下: 代碼如下:# 取前5個字符,跳過4個字符華,再取3個字符 format = '5s 4x 3s' 2. 使用struck.unpack獲取子字符串 代碼如下:import struct print struct.unpack(format, 'Test astring') #('

1. 設置fomat格式,如下:
代碼如下:
# 取前5個字符,跳過4個字符華,再取3個字符
format = '5s 4x 3s'
2. 使用struck.unpack獲取子字符串
代碼如下:
import struct
print struct.unpack(format, 'Test astring')
#('Test', 'ing')
來個簡單的例子吧,有一個字符串'He is not very happy',處理一下,把中間的not去掉,然后再輸出。
代碼如下:
import struct
theString = 'He is not very happy'
format = '2s 1x 2s 5x 4s 1x 5s'
print ' '.join(struct.unpack(format, theString))
輸出結果:
He is very happy
聲明:本網頁內容旨在傳播知識,若有侵權等問題請及時與本網聯系,我們將在第一時間刪除處理。TEL:177 7030 7066 E-MAIL:11247931@qq.com
Pythonstruct.unpack
Pythonstruct.unpack:1. 設置fomat格式,如下: 代碼如下:# 取前5個字符,跳過4個字符華,再取3個字符 format = '5s 4x 3s' 2. 使用struck.unpack獲取子字符串 代碼如下:import struct print struct.unpack(format, 'Test astring') #('