【发布时间】:2020-04-03 13:37:13
【问题描述】:
我创建了一个脚本,该脚本将 .txt 文件中的大量数据以我想要的格式 [3:4:n] 收集到一个数组中,并且信息记录如下(我认为)。 .txt文件就是这种格式
1.000000e-01 1.000000e-01 1.000000e-01
1.000000e-01 2.000000e-01 3.000000e-01
3.000000e-01 2.000000e-01 1.000000e-01
1.000000e-01 2.000000e-01 4.000000e-01
并重复 N 次,我基本上从 4 行存储到 for 行(就像一个块),因为我正在处理来自 STL 部分的 ASCII 文件。
从这个意义上说,我有这个代码:
f = open("camaSTLfinalmente.txt","r")
b_line = 0
Coord = []
Normal = []
Vertice_coord = []
Tri = []
blook = []
for line in f:
line = line.rstrip()
if(line):
split = line.split()
for axis in range(0,3):
if(b_line == 0): #normal
Normal.append(split[axis])
else: #triangulo
Vertice_coord.append(split[axis])
if(b_line > 0):
Tri.append(Vertice_coord)
Vertice_coord = []
if(b_line == 3):
block.append(Normal)
block.append(Tri)
Coord.append(block)
block = []
Normal = []
Tri = []
b_line = 0
else:
b_line+=1
print(Coord[0]) #prints the follow line that I wrote after the code
信息存储方式: [['1.000000e-01', '1.000000e-01', '1.000000e-01'], [['1.000000e-01', '2.000000e-01', '3.000000e-01'], [' 3.000000e-01', '2.000000e-01', '1.000000e-01'], ['1.000000e-01', '2.000000e-01', '-4.000000e-01']]]
有什么办法可以简化吗?
我想借此机会问一下:我想把这些信息转换成数字,理想的情况是读取指数(e)之后的数字并相应地改变数字,即1.000000e-01转到 0,1(为了使用类似的数组进行操作,我在其中存储来自另一个具有相同格式的 .txt 文件的信息)
感谢关注,
佩德罗
【问题讨论】:
标签: python arrays operators coordinate-transformation simplify