本篇僅為邏輯說明
magicStr = “SavingKing” #實際資料為 bytearray
offset = len(magicStr) #10
strdata = magicStr + “ab” + magicStr +”cd” + magicStr + “ef”
#’SavingKingabSavingKingcdSavingKingef’
# 簡化的資料,實際應用可能一長串 bytearray
lis_wanted=[]
while strdata:
nextIdx = strdata.find(magicStr,1)
#len(magicStr) =10
#1改成10也可以跑出一樣的結果
if nextIdx==-1:break
wanted = strdata[offset:offset+2]
#擷取到資料後,再做struct.unpack_from()的動作
#本篇簡化版略過
lis_wanted.append(wanted)
strdata = strdata[nextIdx:]
#print(strdata)
print(lis_wanted)

str.find()的效果:

若搜尋不到則返回-1
if nextIdx==-1:break
搜尋不到則跳出迴圈
簡化版程式輸出:
[‘ab’, ‘cd’]
看起來少抓了最後一個”ef”
但真實資料,最後一個迴圈
因為長度不足,會出現:
error: unpack requires a buffer of 36 bytes
實際應用是正確的
推薦hahow線上學習python: https://igrape.net/30afN
![Python 讀取 DOCX 圖片關聯:qn+find/findall 與 XPath 的實戰對照 from lxml import etree ; from docx.oxml.ns import qn; lxml.etree._Element.findall( f”.//{ qn(‘a:blip’) }” ) ; .get( qn(“r:embed”) ) #獲取 屬性名 ‘r:embed’ 的 屬性值(如: ‘rId4’) ; lxml.etree._Element.xpath( “//a:blip/@r:embed”, namespaces = NS) #/@r:embed = 獲取 屬性名 ‘r:embed’ 的 屬性值(如: ‘rId4’),使用.findall() 要先.findall()獲取List[_Element],再迴圈_Element.get()獲取屬性值, .xpath() 第一個參數path 使用”//a:blip/@r:embed” ,可直接獲取屬性值(List[str]如: [‘rId4’, ‘rId5’]) ; 如何對docx真實移除圖片瘦身? Python 讀取 DOCX 圖片關聯:qn+find/findall 與 XPath 的實戰對照 from lxml import etree ; from docx.oxml.ns import qn; lxml.etree._Element.findall( f”.//{ qn(‘a:blip’) }” ) ; .get( qn(“r:embed”) ) #獲取 屬性名 ‘r:embed’ 的 屬性值(如: ‘rId4’) ; lxml.etree._Element.xpath( “//a:blip/@r:embed”, namespaces = NS) #/@r:embed = 獲取 屬性名 ‘r:embed’ 的 屬性值(如: ‘rId4’),使用.findall() 要先.findall()獲取List[_Element],再迴圈_Element.get()獲取屬性值, .xpath() 第一個參數path 使用”//a:blip/@r:embed” ,可直接獲取屬性值(List[str]如: [‘rId4’, ‘rId5’]) ; 如何對docx真實移除圖片瘦身?](https://i2.wp.com/savingking.com.tw/wp-content/uploads/2025/11/20251119130848_0_3fbf6b.png?quality=90&zoom=2&ssl=1&resize=350%2C233)









你好,我也是遇到了同样的问题,需要将AWR1443上保存的dat文件在python上解码观察数据,是否方便提供一下完整的python代码?如果有需要可以有偿。谢谢