从CSV文件破折号Python读取值

时间:2019-02-07 06:54:57

标签: python python-3.x geopandas plotly-dash

我在csv文件(例如(123.456,789.1234))中有map_geo_location值,如何在破折号中获取值以将其添加为python中的地理分布破折号属性lanlat的值?

1 个答案:

答案 0 :(得分:1)

list.txt:

(123.456,789.1234)
(763.426,659.9834)
(873.566,789.6734)
(773.766,239.234)
(343.776,889.1344)
(873.766,679.1344)

然后:

logFile = "list.txt"
with open(logFile) as f:
    content = f.readlines()

# you may also want to remove empty lines
content = [l.strip() for l in content if l.strip()]

lat = []
long = []

for line in content:
    lat.append(line.rpartition(',')[2].strip(")"))
    long.append(line.rpartition(',')[0].strip("("))


print("Latitude List: {}".format(lat))
print("Longitude List: {}".format(long))

输出:

Latitude List: ['789.1234', '659.9834', '789.6734', '239.234', '889.1344', '679.1344']
Longitude List: ['123.456', '763.426', '873.566', '773.766', '343.776', '873.766']