如何从文本中提取一行作为函数的输入 python

how to take a line from a text to use as input for a function python

大家好,我有一个填充了一些地理数据的文本,如下所示:

[(-76.34666,40.006886),(-76.34666,40.073017),(-76.25411,40.073017),(76.25411,40.006886)] [(-84.82031,38.403187)),(-84.82031,42.327133),(-80.51862,42.327133),(-80.51862,38.403187)]

现在我想将它逐行作为多边形函数的输入。在我尝试迭代之前,我首先尝试用一行来制作它,但它不会工作。这是我现在的代码:

    from shapely.wkt import loads as load_wkt
    from shapely.geometry import Point, Polygon

    f = open('koordinat.txt', 'r')
    line = f.readline()
    p = Polygon(line)
    print (p.centroid)

"p = Polygon(line)" 和 "A LinearRing must have at least 3 coordinate tuples" 但是当把其中一行放在功能手册中时,它工作正常。

有什么帮助吗?还有一个可能的迭代的例子会很好:)

您正在向 Polygon 传递一个字符串,但它需要一个坐标(数字)列表。

试试这个:

import ast
line = ast.literal_eval(f.readline())