看板 Python 關於我們 聯絡資訊
搭好~如附圖所示,我想要從一個‘’字串‘’抓出score的值,也就是80,雖然這樣寫 可行,但是想知道有更好看或更有效率的方法嗎? 我是有查了一下字串處理的資料,好像正規表示法re的效能上沒有比較好..? https://i.imgur.com/1hSEwpB.jpg -- ※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 118.171.168.174 (臺灣) ※ 文章網址: https://www.ptt.cc/bbs/Python/M.1575212524.A.8A8.html
hongyan: 固定這樣格式的話 find("score") find('},')後 str[:]切 12/01 23:31
hongyan: 切片 12/01 23:31
hongyan: slice1 = s.find(":", s.find('score')) + 2 12/01 23:45
hongyan: slice2 = s.find("}", s.find('score')) - 1 12/01 23:45
hongyan: result = s[slice1:slice2] 12/01 23:46
hoho8: https://i.imgur.com/iSFM3ho.png 12/02 00:34
buddy66: 哇靠!感謝兩位大神 12/02 00:52
froce: https://tinyurl.com/t7uqc77 12/03 19:31
froce: 另外這一看就是變形的JSON,整個去做json.loads比較好處理 12/03 19:39