python{"code":200,"message":"成功","data":{"access_token":"eyJh} 怎么把access_token对应的值提取
时间: 2023-07-19 11:46:45 浏览: 121
你想要提取字符串中access_token对应的值,可以使用正则表达式来实现。
示例代码如下:
```python
import re
response = '{"code":200,"message":"成功","data":{"access_token":"eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJleHAiOjE2ODY2NDUwMzIsInVzZXJfbmFtZSI6IjAwMzgzMSIsImp0aSI6ImQwZmUyMzgwLTc2MzEtNDA1Ny04NWY5LWM4ZTk1ZWNmMWE5ZSIsImNsaWVudF9pZCI6ImJhc2V1cyIsInNjb3BlIjpbImFsbCJdfQ.lMKp"}}'
# 定义正则表达式,匹配access_token的值
pattern = '"access_token":"(.*?)"'
# 提取access_token的值
access_token = re.findall(pattern, response)[0]
print(access_token)
```
输出:
```
eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJleHAiOjE2ODY2NDUwMzIsInVzZXJfbmFtZSI6IjAwMzgzMSIsImp0aSI6ImQwZmUyMzgwLTc2MzEtNDA1Ny04NWY5LWM4ZTk1ZWNmMWE5ZSIsImNsaWVudF9pZCI6ImJhc2V1cyIsInNjb3BlIjpbImFsbCJdfQ.lMKp
```
可以看到,我们使用正则表达式成功提取了access_token的值。
阅读全文