我认为csv模块更适合解析此类数据:
import csv
siteData = """
cat|x|y|z
1|343741.396330097|1029255.04807763|0
2|342270.660062496|1030198.57861216|0
3|339743.874252208|1030705.47801259|0
4|343031.709457817|1027263.37839108|0
5|339453.578589758|1029954.65896125|0
6|341376.669330771|1029549.5339487|0
7|339224.904513178|1030438.74647616|0
8|340285.317292692|1029431.45860403|0
9|343135.800101739|1029804.27059289|0
10|338982.428513304|1028857.42081259|0
11|338405.612076696|1034287.30455498|0
12|338080.457138754|1034337.35427931|0
13|338944.90541069|1034519.90737124|0
14|340963.209546547|1032528.77710582|0
15|337891.948788692|1030626.74686363|0
"""
points = []
reader = csv.DictReader(siteData.strip().splitlines(), delimiter="|", quotechar='"')
for row in reader:
point = {
"id": int(row["cat"]),
"x": float(row["x"]),
"y": float(row["y"]),
"z": float(row["z"])
}
points.append(point)
for point in points:
print(point)
输出:
{'id': 1, 'x': 343741.396330097, 'y': 1029255.04807763, 'z': 0.0}
{'id': 2, 'x': 342270.660062496, 'y': 1030198.57861216, 'z': 0.0}
{'id': 3, 'x': 339743.874252208, 'y': 1030705.47801259, 'z': 0.0}
{'id': 4, 'x': 343031.709457817, 'y': 1027263.37839108, 'z': 0.0}
{'id': 5, 'x': 339453.578589758, 'y': 1029954.65896125, 'z': 0.0}
{'id': 6, 'x': 341376.669330771, 'y': 1029549.5339487, 'z': 0.0}
{'id': 7, 'x': 339224.904513178, 'y': 1030438.74647616, 'z': 0.0}
{'id': 8, 'x': 340285.317292692, 'y': 1029431.45860403, 'z': 0.0}
{'id': 9, 'x': 343135.800101739, 'y': 1029804.27059289, 'z': 0.0}
{'id': 10, 'x': 338982.428513304, 'y': 1028857.42081259, 'z': 0.0}
{'id': 11, 'x': 338405.612076696, 'y': 1034287.30455498, 'z': 0.0}
{'id': 12, 'x': 338080.457138754, 'y': 1034337.35427931, 'z': 0.0}
{'id': 13, 'x': 338944.90541069, 'y': 1034519.90737124, 'z': 0.0}
{'id': 14, 'x': 340963.209546547, 'y': 1032528.77710582, 'z': 0.0}
{'id': 15, 'x': 337891.948788692, 'y': 1030626.74686363, 'z': 0.0}