1

我正在从网上阅读一个文本文件。该文件以一些包含数据点数量的标题行开头,然后是实际顶点(每个顶点 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() 我想读取文件中指定数量的元素

欢迎任何建议..

4

2 回答 2

3

从您的解释中,我不完全确定您的目标是什么。

作为记录,这里的代码与您的代码基本相同,似乎正在尝试使用一些我会使用的技术而不是您选择的技术。如果您使用 while 循环和索引,通常表明您做错了什么,并且您的代码确实不起作用,因为contents[j] = ...将是一个IndexError.

lines = (line.strip().lower() for line in your_web_page)

points_line = next(line for line in lines if 'points' in line)
_, node_number, node_type = points_line.split()
node_number = int(node_number)

def get_contents(lines):
    for line in lines:
        if 'polygons' in line:
            break

        for number in line.split():
            yield float(number)

contents = list(get_contents(lines))

如果您对自己想做的新事物更加明确,也许有人可以为您的最终目标提供更好的答案。

于 2010-04-20T23:40:06.237 回答
0

这是您的代码的简单清理,它应该使循环内容更快。

ur=urlopen("http://.../file.dat")
contents = []
node_number = 0
node_type = None
while 1:
    line = ur.readline()
    if not line:
        break
    line = line.lower()       
    if 'points' in line :
        word = line.split()
        node_number = int(word[1])
        node_type = word[2]
        while 1:
            pieces = ur.readline().split()
            if not pieces: continue # or break or issue error message
            if pieces[0].lower() == 'polygons': break
            contents.extend(map(float, pieces))
assert len(contents) == node_number * 3

如果您将代码包装在一个函数中并调用它,它将运行得更快(因为您将访问局部变量而不是全局变量)。

请注意,最重要的更改在脚本的结尾附近/结尾处。

但是:退后一步,想一想: ur.readline() 占用了多少时间,拆线占用了多少时间?

于 2010-04-21T00:04:22.277 回答