简体   繁体   English

如何在python中找到字符串和整数数据

[英]How do I locate string and integer data in python

I would like to locate this data from a page source 我想从页面源中找到此数据

"location": {"latitude": 35.4677026, "longitude": -80.6093396}

currently I am running this code 目前我正在运行此代码

floats = re.findall(r"[-+]?\d+\.\d+", str(soup))
  for i in range(len(floats)):
      if len(floats[i]) > 9:
        LatLong.append(floats[i])

The output has the data I am looking for, but it also has data I don't want with it mixed in 输出包含我要查找的数据,但也包含我不想要的数据

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'])

You should verify if the used keys exist in the data dictionary for avoid dictionary keys errors 您应该验证数据字典中是否存在使用过的键,以避免字典键错误

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM