本篇僅為邏輯說明
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 檢驗字符串格式:掌握正則表達式(Regular Expression)的起始^與終止$符號, pattern = r’^GATR[0-9]{4}$’ 使用 Python 檢驗字符串格式:掌握正則表達式(Regular Expression)的起始^與終止$符號, pattern = r’^GATR[0-9]{4}$’](https://i0.wp.com/savingking.com.tw/wp-content/uploads/2024/07/20240712093637_0.png?quality=90&zoom=2&ssl=1&resize=350%2C233)

![Python: pandas.Series如何只保留str,去除重複值?#isinstance(x:Any, str) -> bool #.drop_duplicates() #Series.apply( function )逐元素應用function運算 #DataFrame.apply( function )逐Series應用function運算 .drop_duplicates() 跟.unique()有何差別? df.drop_duplicates() 等效於 df[~df.duplicated()] Python: pandas.Series如何只保留str,去除重複值?#isinstance(x:Any, str) -> bool #.drop_duplicates() #Series.apply( function )逐元素應用function運算 #DataFrame.apply( function )逐Series應用function運算 .drop_duplicates() 跟.unique()有何差別? df.drop_duplicates() 等效於 df[~df.duplicated()]](https://i1.wp.com/savingking.com.tw/wp-content/uploads/2024/11/20241123194900_0_5218de.png?quality=90&zoom=2&ssl=1&resize=350%2C233)
![Python爬蟲:BeautifulSoup的 .find_all() 與 .find() 與 .select(‘標籤名[屬性名1=”屬性值1″][屬性名2=”屬性值2″]’) ; from bs4 import BeautifulSoup ; Live Server(可以預覽HTML的VS Code套件) Python爬蟲:BeautifulSoup的 .find_all() 與 .find() 與 .select(‘標籤名[屬性名1=”屬性值1″][屬性名2=”屬性值2″]’) ; from bs4 import BeautifulSoup ; Live Server(可以預覽HTML的VS Code套件)](https://i2.wp.com/savingking.com.tw/wp-content/uploads/2025/03/20250330190318_0_925655.jpg?quality=90&zoom=2&ssl=1&resize=350%2C233)



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