例如:ret={"code": 200, "data": "OK"} 或 {"code": 403, "data": "FAIL"} 或 {"code": [其他任何可能的返回编号], "data": "[其他任何可能的data]"}

现在需要把 ret 中的 data 的值取出来,那么我们可以用正则替换 re.sub 来实现:

import re

re.sub('\'?\s*}.*','',re.sub('.*data\'?:\s*\'?','',str(ret).replace('"','')))

首先通过常规替换 replace,去除双引号:str(ret).replace('"','')

然后通过 re.sub(regex, str, string) 去除 data: 及其前面的内容

最后通过 re.sub(regex, str, string) 去除末尾的 } 等其他的内容。

 

相关文章:

  • 2021-07-28
  • 2022-12-23
  • 2021-12-24
  • 2022-12-23
  • 2022-12-23
  • 2022-02-08
  • 2021-12-24
猜你喜欢
  • 2021-08-04
  • 2022-12-23
  • 2021-08-25
  • 2022-12-23
  • 2021-12-24
  • 2022-01-07
  • 2021-10-26
相关资源
相似解决方案