English 中文(简体)
• 如何读出花名册上的具体浮标数?
原标题:how to read specific number of floats from file in python?

我正在阅读网上的文本文件。 档案中有一些显示数据点数的头条线,沿用了实际的曲线(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 载有vert号(在这种情况下,每行有3vert,但可以改变)

这就是我现在如何读一下:

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

我怎么能够读到一定数量的浮标,而不是按线阅读,作为浮标和转换为浮标数?

不是尿线。 我想读一下档案中具体内容的编号。

任何建议都值得欢迎。

问题回答

我并不完全确定你的目标来自你们的解释。

就记录而言,这部法典与你似乎试图使用某些技术的做法基本相同。 这通常表明,如果你在休息和指数以及实际上你的代码不奏效时重新使用,你会再做一些错误的事情,因为<代码>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))

如果你对你想要做的新事情有更明确的认识,那么也许有人能为你的最终目标提供更好的答案。

这里不清你们的法典,这应当使内容更快捷。

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

如果你把该守则总结成一项功能,并称该守则将更加快捷(因为你将获得当地变量而不是全球变量)。

请注意,最重大的变化接近/接近文字末。

HOWEVER:站起来,思考一下这几秒:有多少时间由导游接手,有多少时间通过拆解线?





相关问题
Can Django models use MySQL functions?

Is there a way to force Django models to pass a field to a MySQL function every time the model data is read or loaded? To clarify what I mean in SQL, I want the Django model to produce something like ...

An enterprise scheduler for python (like quartz)

I am looking for an enterprise tasks scheduler for python, like quartz is for Java. Requirements: Persistent: if the process restarts or the machine restarts, then all the jobs must stay there and ...

How to remove unique, then duplicate dictionaries in a list?

Given the following list that contains some duplicate and some unique dictionaries, what is the best method to remove unique dictionaries first, then reduce the duplicate dictionaries to single ...

What is suggested seed value to use with random.seed()?

Simple enough question: I m using python random module to generate random integers. I want to know what is the suggested value to use with the random.seed() function? Currently I am letting this ...

How can I make the PyDev editor selectively ignore errors?

I m using PyDev under Eclipse to write some Jython code. I ve got numerous instances where I need to do something like this: import com.work.project.component.client.Interface.ISubInterface as ...

How do I profile `paster serve` s startup time?

Python s paster serve app.ini is taking longer than I would like to be ready for the first request. I know how to profile requests with middleware, but how do I profile the initialization time? I ...

Pragmatically adding give-aways/freebies to an online store

Our business currently has an online store and recently we ve been offering free specials to our customers. Right now, we simply display the special and give the buyer a notice stating we will add the ...

Converting Dictionary to List? [duplicate]

I m trying to convert a Python dictionary into a Python list, in order to perform some calculations. #My dictionary dict = {} dict[ Capital ]="London" dict[ Food ]="Fish&Chips" dict[ 2012 ]="...

热门标签