如何从文本中检索值

时间:2019-05-02 12:04:30

标签: python

我正在尝试从看起来像这样的txt文件中检索X_coordinates和Y_coordinates:

text =POLYGON ((252291.12483051314 1116798.7306648178,294319.54664371524 1122497.4997242352,297881.2773058511 1084030.8085731687,296456.5850409968 1022769.0411844333,240893.58671167865 988576.4268279299,190317.0113093506 957945.5431335622,188179.97291206918 985014.696165794,274373.8549357549 1048413.5019518109,252291.12483051314 1116798.7306648178))

在将X和Y坐标分割的值之间有一个空格。

我已经尝试过:

     X_coord = []
     Y_coord = []
     for i in range (10 ,len(text)-2):
       Flag_X = True
       Flag_Y = False
       if (text[i] != " " and Flag_X == True) : 
          X_coord += (text[i])
       if (text[i] == " "):
          Flag_X = False
          Flag_Y = True
          X_coord += ","
       if (text[i] != "," and Flag_Y == True):
          Y_coord += text[i]
       if (text[i] == ","):
          Flag_X = True
          Flag_Y = False
          Y_coord += ","

代码给出了输出['2',5','2',...],我需要这样的输出

X_coordinates= [252291.12483051314, 294319.54664371524 … ]
Y_coordinates = [1116798.7306648178, 1122497.4997242352 ... ]

2 个答案:

答案 0 :(得分:1)

使用str.stripstr.split

例如:

X_coord = []
Y_coord = []
with open(filename) as infile:
    for line in infile:
        for cords in line.lstrip("POLYGON").strip().strip("()").split(","):
            x, y = cords.split()
            X_coord.append(float(x))
            Y_coord.append(float(y))
print(X_coord)
print(Y_coord)

输出:

[252291.12483051314, 294319.54664371524, 297881.2773058511, 296456.5850409968, 240893.58671167865, 190317.0113093506, 188179.97291206918, 274373.8549357549, 252291.12483051314]
[1116798.7306648178, 1122497.4997242352, 1084030.8085731687, 1022769.0411844333, 988576.4268279299, 957945.5431335622, 985014.696165794, 1048413.5019518109, 1116798.7306648178]

Txt中的内容

POLYGON ((252291.12483051314 1116798.7306648178,294319.54664371524 1122497.4997242352,297881.2773058511 1084030.8085731687,296456.5850409968 1022769.0411844333,240893.58671167865 988576.4268279299,190317.0113093506 957945.5431335622,188179.97291206918 985014.696165794,274373.8549357549 1048413.5019518109,252291.12483051314 1116798.7306648178))

答案 1 :(得分:0)

如果不需要列表,则可以使用此单行代码(它返回元组)

flow = ET.SubElement(
    routes, 
    "flow", 
     **{
        "id": str(i), 
        "from": f["source"], 
        "to": f["sink"]
     }
)

输出:

coordinates_x, coordinates_y = zip(*(coord.split(" ") for coord in text[10:-2].split(",")))