首页 > 解决方案 > 从列表中提取确切的值

问题描述

我怎样才能从中提取经度和纬度 -

Feature(place='夏威夷火山 SSE 12km', long=-155.2005, lat=19.3258333, depth=6.97, mag=5.54)

部分代码如下

lrgst = features[0]
print ('\n',lrgst)
plt.bar(y_pos, magn)
plt.xticks(y_pos, loc)
plt.ylabel('Magnitude')
plt.show()


features = list(get_info()) #Storing our json information into a list 'Features'

标签: pythonjsonlistsplitextract

解决方案


假设“功能”是给出示例的条目列表,则此解决方案将起作用:

features = ["Feature(place='12km SSE of Volcano, Hawaii', long=-155.2005, lat=19.3258333, depth=6.97, mag=5.54)"]

def find(string, char_before, char_after):
    start = string.find(char_before) + len(char_before)
    end = string[start:].find(char_after) + start
    return string[start:end]

long = find(features[0], 'long=', ', ')
lat = find(features[0], 'lat=', ', ')

print(lat + ', ' + long)

    19.3258333, -155.2005

推荐阅读