我正在从网上阅读一个文本文件。该文件以一些包含数据点数量的标题行开头,然后是实际顶点(每个顶点 3 个坐标)。该文件如下所示:
# comment
HEADER TEXT
POINTS 6 float
1.1 2.2 3.3 4.4 5.5 6.6 7.7 8.8 9.9
1.1 2.2 3.3 4.4 5.5 6.6 7.7 8.8 9.9
POLYGONS
以单词开头的行POINTS
包含顶点数(在这种情况下,我们每行有 3 个顶点,但这可能会改变)
这就是我现在阅读的方式:
ur=urlopen("http://.../file.dat")
j=0
contents = []
while 1:
line = ur.readline()
if not line:
break
else:
line=line.lower()
if 'points' in line :
myline=line.strip()
word=myline.split()
node_number=int(word[1])
node_type=word[2]
while 'polygons' not in line :
line = ur.readline()
line=line.lower()
myline=line.split()
i=0
while(i<len(myline)):
contents[j]=float(myline[i])
i=i+1
j=j+1
如何读取指定数量的浮点数,而不是逐行读取字符串并转换为浮点数?
而不是 ur.readline() 我想读取文件中指定数量的元素
欢迎任何建议..