看板 Python 關於我們 聯絡資訊
In [1] tmp.content Out [1] b'[\n{"_id":"DUT2236-bkcin128-11032016","InternetGatewayDevice": {"DeviceInfo":{"SWV":{"_object":false,"_value":"0.02.01", "_type":"xsd:string","_timestamp":"2018-11-07T13:47:17.373Z","_writable":false}}}}\n]' 現在我的程式碼如下: import re In [1] test_string = 'find: tmp.content' pattern = 'DeviceInfo | SWV | 0.02.01' ans=re.findall(pattern,test_string) print(ans) Out [1] [] 出現的結果為空? 為何在Out [1] 沒有出現DeviceInfo.SoftwareVersion 0.02.01的訊息? 想請問一下,我是少做了甚麼? 懇請指點<(_ _)> -- ※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 36.224.53.188 ※ 文章網址: https://www.ptt.cc/bbs/Python/M.1541599781.A.983.html ※ 編輯: han999 (36.224.53.188), 11/07/2018 22:13:02
vi000246: | 這個符號應該要escape 我猜啦 11/07 22:15
vi000246: 空格改用\s看看 11/07 22:15
djshen: 你有看過文件? 11/07 22:15
djshen: 然後你應該print test_string看是什麼 11/07 22:16
edwar: 大概是少看了re.findall的文件 11/07 22:34
s860134: 資料一看就是 json ,為何不用 json.loads ? 11/07 22:38
s860134: import json;_j = json.loads(tmp.content) 11/07 22:50
s860134: print(_j[0]['DeviceInfo']['SWV']['_value']) 11/07 22:52
s860134: re 的用法找一下吧 ,語法錯誤甚至不知道你要切啥... 11/07 22:53
s860134: 更正一下上面的 key ['InternetGatewayDevice']['DeviceI 11/07 22:58
s860134: ... 後略,資料隔是既然是 json 用 json.loads 就會 11/07 22:58
s860134: 很原味的轉成 python 的內建類別了 11/07 22:58
s860134: 當然若是遇到一些空值或編碼,要加點 flag 去調整 11/07 22:59
dpes10087: 'DeviceInfo | SWV | 0.02.01',把中間的空格刪掉。 11/08 09:04
dpes10087: 這個網站https://regex101.com/ 或 Notepad++進行測試 11/08 09:07
Kazimir: 'find: tmp.content' ??? 所以這是一整個字串? 11/08 22:15
supershi: test_string改成f'find: {tmp.content}'看看 11/09 06:54
renshin: 感覺起來test_string是一般字元而不是檔案內容 11/11 11:25