如何将此文件格式化为可以在matplotlib中绘制的坐标列表?

时间:2014-09-18 20:54:07

标签: python list python-3.x file-io graphics

我有一个带坐标的文件来绘制邻域。每对坐标由管道分隔每个社区用加+分隔。我有一个包含以下行的文件:

40.742742,-73.993847|40.739389,-73.985667|40.74715499999999,-73.97992|40.750573,-73.988415|40.742742,-73.993847+40.734706,-73.991915|40.736917,-73.990263|40.736104,-73.98846|40.740315,-73.985263|40.74364800000001,-73.993353|40.73729099999999,-73.997988|40.734706,-73.991915+40.729226,-74.003463|40.7214529,-74.006038|40.717745,-74.000389|40.722299,-73.996634|40.725291,-73.994413|40.729226,-74.003463
40.75905000000001,-73.992917|40.75330999999999,-73.997111|40.747842,-73.984165|40.750541,-73.982105|40.7536289,-73.979873|40.75905000000001,-73.992917+40.734706,-73.991915|40.736917,-73.990263|40.736104,-73.98846|40.740315,-73.985263|40.74364800000001,-73.993353|40.73729099999999,-73.997988|40.734706,-73.991915+40.76522500000001,-73.988411|40.761590000000005,-73.979911|40.75479899999999,-73.985196|40.753337,-73.981633|40.748297,-73.985624|40.745534,-73.979058|40.751012,-73.975218|40.7623729,-73.967169|40.76874000000001,-73.982489|40.767305,-73.983433|40.76845,-73.985873|40.76522500000001,-73.988411
40.742742,-73.993847|40.739389,-73.985667|40.74715499999999,-73.97992|40.750573,-73.988415|40.742742,-73.993847+40.75065,-74.00914|40.74859,-74.00845|40.74211100000001,-74.009657|40.736667,-73.99649800000002|40.747337,-73.988864|40.75538,-74.007445|40.75065,-74.00914+40.734706,-73.991915|40.736917,-73.990263|40.736104,-73.98846|40.740315,-73.985263|40.74364800000001,-73.993353|40.73729099999999,-73.997988|40.734706,-73.991915

假设以上是整个文件,我想要以下输出:

[[[40.742742,-73.993847], [40.739389,-73.985667], [40.74715499999999,-73.97992], [40.750573,-73.988415], [40.742742,-73.993847]], [[40.734706,-73.991915], [40.736917,-73.990263], [40.736104,-73.98846], [40.740315,-73.985263], [40.74364800000001,-73.993353], [40.73729099999999,-73.997988], [40.734706,-73.991915]], [[40.729226,-74.003463], [40.7214529,-74.006038], [40.717745,-74.000389], [40.722299,-73.996634], [40.725291,-73.994413], [40.729226,-74.003463]], [[40.75905000000001,-73.992917], [40.75330999999999,-73.997111], [40.747842,-73.984165], [40.750541,-73.982105], [40.7536289,-73.979873], [40.75905000000001,-73.992917]], [[40.734706,-73.991915], [40.736917,-73.990263], [40.736104,-73.98846], [40.740315,-73.985263], [40.74364800000001,-73.993353], [40.73729099999999,-73.997988], [40.734706,-73.991915]], [[40.76522500000001,-73.988411], [40.761590000000005,-73.979911], [40.75479899999999,-73.985196], [40.753337,-73.981633], [40.748297,-73.985624], [40.745534,-73.979058], [40.751012,-73.975218], [40.7623729,-73.967169], [40.76874000000001,-73.982489], [40.767305,-73.983433], [40.76845,-73.985873], [40.76522500000001,-73.988411]], [[40.742742,-73.993847], [40.739389,-73.985667], [40.74715499999999,-73.97992], [40.750573,-73.988415], [40.742742,-73.993847]], [[40.75065,-74.00914], [40.74859,-74.00845], [40.74211100000001,-74.009657], [40.736667,-73.99649800000002], [40.747337,-73.988864], [40.75538,-74.007445], [40.75065,-74.00914]], [[40.734706,-73.991915], [40.736917,-73.990263], [40.736104,-73.98846], [40.740315,-73.985263], [40.74364800000001,-73.993353], [40.73729099999999,-73.997988], [40.734706,-73.991915]]]

我可以使用以下代码在matplotlib中绘制曼哈顿的图片:

import matplotlib.pyplot as plt

mVerts = []
with open('Manhattan_Coords.txt') as f:
    for line in f:
        pair = [float(s) for s in line.strip()[1:-1].split(", ")]
        mVerts.append(pair)

plt.plot(*zip(*mVerts))
plt.show()

文件Manhattan_Coords.txt包含以下行:

[-74.0104294, 40.6996416]
[-74.0048504, 40.701854]
[-74.0000439, 40.7046519]

我需要将每个邻域保持为一个单独的多边形,在我的文件中用+或换行符分隔(文件中使用了两种方式)

1 个答案:

答案 0 :(得分:0)

如果您想根据newline+

分组
import re
with open('Manhattan_Coords.txt') as f:
      sections = ([list(map(float, (ele.split(","))))
             for li in (x.split("|")
                        for x in re.split("\n|\+", f.read().replace("-", "-0").replace("-\\r", "0").rstrip()))
             for ele in li])
  [[40.742742, -73.993847], [40.739389, -73.985667], [40.74715499999999, -73.97992], [40.750573, -73.988415], [40.742742, -73.993847], [40.734706, -73.991915], [40.736917, -73.990263], [40.736104, -73.98846], [40.740315, -73.985263], [40.74364800000001, -73.993353], [40.73729099999999, -73.997988], [40.734706, -73.991915], [40.729226, -74.003463], [40.7214529, -74.006038], [40.717745, -74.000389], [40.722299, -73.996634], [40.725291, -73.994413], [40.729226, -74.003463], [40.75905000000001, -73.992917], [40.75330999999999, -73.997111], [40.747842, -73.984165], [40.750541, -73.982105], [40.7536289, -73.979873], [40.75905000000001, -73.992917], [40.734706, -73.991915], [40.736917, -73.990263], [40.736104, -73.98846], [40.740315, -73.985263], [40.74364800000001, -73.993353], [40.73729099999999, -73.997988], [40.734706, -73.991915], [40.76522500000001, -73.988411], [40.761590000000005, -73.979911], [40.75479899999999, -73.985196], [40.753337, -73.981633], [40.748297, -73.985624], [40.745534, -73.979058], [40.751012, -73.975218], [40.7623729, -73.967169], [40.76874000000001, -73.982489], [40.767305, -73.983433], [40.76845, -73.985873], [40.76522500000001, -73.988411], [40.742742, -73.993847], [40.739389, -73.985667], [40.74715499999999, -73.97992], [40.750573, -73.988415], [40.742742, -73.993847], [40.75065, -74.00914], [40.74859, -74.00845], [40.74211100000001, -74.009657], [40.736667, -73.99649800000002], [40.747337, -73.988864], [40.75538, -74.007445], [40.75065, -74.00914], [40.734706, -73.991915], [40.736917, -73.990263], [40.736104, -73.98846], [40.740315, -73.985263], [40.74364800000001, -73.993353], [40.73729099999999, -73.997988], [40.734706, -73.991915]]