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