python - 如何在python中定位字符串和整数数据
问题描述
我想从页面源中找到这些数据
"location": {"latitude": 35.4677026, "longitude": -80.6093396}
目前我正在运行此代码
floats = re.findall(r"[-+]?\d+\.\d+", str(soup))
for i in range(len(floats)):
if len(floats[i]) > 9:
LatLong.append(floats[i])
输出有我正在寻找的数据,但它也有我不想要的数据混入其中
解决方案
import json
string_data = '{"location": {"latitude": 35.4677026, "longitude": -80.6093396}}'
data = json.loads(string_data)
if 'location' in data:
# do something with the data
print(data['location']['latitude'])
print(data['location']['longitude'])
您应该验证数据字典中是否存在使用的键以避免字典键错误
推荐阅读
- mongodb - mongoDB, mongoose - 聚合对象数组
- edge-detection - 二值图像坐标错误
- reactjs - 在功能组件上设置 ref
- node.js - 尝试通过角色名 [discord.js] 获取角色对象时,我不断收到错误消息
- java - 无法在 VSCode 中的包之间拖放文件
- docker - docker容器上的多个站点
- websocket - 使用 websockets 连接两个 node.js 服务器(ws 包)(https)
- java - 从 BufferedReader 将值存储在多维数组中
- mysql - 为什么 MySQl 中有左连接和右连接?
- deep-learning - Google Collab 因内存不足而不断崩溃