我有一个VRML文件,其中一部分是这样的:
color Color { color [
.566 .566 1
.629 .629 1
.066 .066 1
.129 .129 1
.691 .691 1
.754 .754 1
.816 .816 1
.504 .504 1
.441 .441 1
.191 .191 1
1 .879 .879
.254 .254 1
1 .754 .754
1 1 1
.879 .879 1
1 .816 .816
.379 .379 1
.316 .316 1
1 .504 .504
1 .691 .691
1 .191 .191
1 .566 .566
1 .629 .629
1 .441 .441
1 .379 .379
1 .066 .066
1 .129 .129
1 0 0
1 .316 .316
0 0 1
1 .254 .254
] }
我在线阅读,并把它放在一个名为'line'的字符串中。
如何从一条线上提取三个花车? 谢谢。
答案 0 :(得分:0)
你可以从包含三个" float-able"的那些行中获取浮点数。字符串使用:
line_floats = [float(field) for field in line.split.()]
或您可以执行的整个文件:
def is_three_floats(line):
fields = line.split()
if len(fields) != 3: return False
try:
line_floats = [float(field) for field in fields]
except ValueError:
return False
return line_floats
float_lines = [is_three_floats(line) for line in fp if is_three_floats(line)]