将一组坐标从字符串转换为整数
convert a set of coordinates from string to int
如何将我的 siteData(坐标列表)从字符串转换为 int?
print(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
根据 Hozayfa 的评论,您可以按如下方式执行此操作:
lineWise = [line.strip() for line in siteData.split('\n')][1:]
# This creates a list containing each row of siteData as a separate item and skips the header
for row in lineWise:
data = row.split('|')
cat = int(data[0])
x = int(float(data[1]))
y = int(float(data[2]))
z = int(float(data[3]))
您可以在循环内使用变量 cat, x, y
和 z
;它们将是 int
.
类型
编辑:这是假设 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
我认为 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}
使用Pandas
import pandas as pd
from io import StringIO
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"""
# wrap the siteData string data in StringIO function
# use pandas read_csv to create DataFrame
df = pd.read_csv(StringIO(siteData), sep ="|", dtype=float).astype(int)
print(df)
输出
cat x y z
0 1 343741 1029255 0
1 2 342270 1030198 0
2 3 339743 1030705 0
3 4 343031 1027263 0
4 5 339453 1029954 0
5 6 341376 1029549 0
6 7 339224 1030438 0
7 8 340285 1029431 0
8 9 343135 1029804 0
9 10 338982 1028857 0
10 11 338405 1034287 0
11 12 338080 1034337 0
12 13 338944 1034519 0
13 14 340963 1032528 0
14 15 337891 1030626 0
如何将我的 siteData(坐标列表)从字符串转换为 int?
print(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
根据 Hozayfa 的评论,您可以按如下方式执行此操作:
lineWise = [line.strip() for line in siteData.split('\n')][1:]
# This creates a list containing each row of siteData as a separate item and skips the header
for row in lineWise:
data = row.split('|')
cat = int(data[0])
x = int(float(data[1]))
y = int(float(data[2]))
z = int(float(data[3]))
您可以在循环内使用变量 cat, x, y
和 z
;它们将是 int
.
编辑:这是假设 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
我认为 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}
使用Pandas
import pandas as pd
from io import StringIO
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"""
# wrap the siteData string data in StringIO function
# use pandas read_csv to create DataFrame
df = pd.read_csv(StringIO(siteData), sep ="|", dtype=float).astype(int)
print(df)
输出
cat x y z
0 1 343741 1029255 0
1 2 342270 1030198 0
2 3 339743 1030705 0
3 4 343031 1027263 0
4 5 339453 1029954 0
5 6 341376 1029549 0
6 7 339224 1030438 0
7 8 340285 1029431 0
8 9 343135 1029804 0
9 10 338982 1028857 0
10 11 338405 1034287 0
11 12 338080 1034337 0
12 13 338944 1034519 0
13 14 340963 1032528 0
14 15 337891 1030626 0