x, y, z = [], [], []
with open("C:\Users\Administrator\Desktop\listpoint\274picking_list.txt ", 'r') as A:
content = A.read()
contract = content.split()
length = len(contract)
print(length)
for index in range(0, length):
str1 = contract[index]
str2 = str1.replace(",", " ")
str3 = str2.split()
# print(type(str3[1]))
x.append(str3[1])
# print(x)
y.append(str3[2])
# print(y)
z.append(str3[3])
# print(z)
2.将点按索引分奇偶–使相邻点一一对应
# 读出的列表值为str型,将str转为int
px = list(map(float, x))
py = list(map(float, y))
pz = list(map(float, z))
# 将读出的x,y,z分量分奇、偶分别存入两个不同列表
x_odd = px[0:length:2]
x_even = px[1:length:2]
y_odd = py[0:length:2]
y_even = py[1:length:2]
z_odd = pz[0:length:2]
z_even = pz[1:length:2]
3.求相邻两点间的距离
t = int(length/2)
dist_final_list = []
for index in range(0, t):
dist = (x_even[index]-x_odd[index])**2+(y_even[index]-y_odd[index])**2+(z_even[index]-z_odd[index])**2
# print(dist)
dist_final = math.sqrt(dist)
dist_final_list.append(dist_final)
# print(dist_final_list)
4.将数据类型转为str型
# 写入txt的数据类型需为str型,通过map将float转为str
list_index = [] # 索引
for index in range(1, t+1):
list_index.append(index)
list_index = list(map(str, list_index))
# print(list_index)
dist_final_list = list(map(str, dist_final_list))
x_odd = list(map(str, x_odd))
y_odd = list(map(str, y_odd))
z_odd = list(map(str, z_odd))
x_even = list(map(str, x_even))
y_even = list(map(str, y_even))
z_even = list(map(str, z_even))
5.将多个列表对应的值写入txt文件
with open("C:\Users\Administrator\Desktop\listpoint\274_list1.txt", "a+") as f:
for i in range(0, t):
if list_index[i] == '/0' or x_odd[i] == '' or y_odd[i] == '' or z_odd[i] == '' or x_even[i] == '' or y_even[i] == '' or z_even[i] == '':
break
f.write('{:},{:},{:},{:},{:},{:},{:},{:}\n'.format(list_index[i], x_odd[i], y_odd[i], z_odd[i], x_even[i], y_even[i], z_even[i], dist_final_list[i]))
f.close()
结果
附测试数据(截图版)
注:本次坐标数据来源于火星点云,意在求取火星石块的直径
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)