Python struct.unpack
更新時間:2008年09月06日 14:07:52 作者:
Python中按一定的格式取出某字符串中的子字符串,使用struck.unpack是非常高效的。
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))
輸出結(jié)果:
He is very happy
復制代碼 代碼如下:
# 取前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))
輸出結(jié)果:
He is very happy
相關文章
python 實用工具狀態(tài)機transitions
這篇文章主要介紹了python 實用工具狀態(tài)機transitions的使用,幫助大家更好的理解和學習python,感興趣的朋友可以了解下2020-11-11citespace數(shù)據(jù)處理:用python對Ref文檔進行去重方式
這篇文章主要介紹了citespace數(shù)據(jù)處理:用python對Ref文檔進行去重方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-11-11Python爬蟲模擬登陸嗶哩嗶哩(bilibili)并突破點選驗證碼功能
這篇文章主要介紹了Python爬蟲模擬登陸嗶哩嗶哩(bilibili)并突破點選驗證碼功能,本文通過圖文實例相結(jié)合給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-12-12