【问题标题】:Attribute error float object has no attribute 'append'属性错误浮动对象没有属性“附加”
【发布时间】:2015-12-27 08:54:47
【问题描述】:

我尝试对打开 LAS 文件的代码进行更改,然后将结果保存在新的 LAS 文件中。 las 文件包含具有坐标(X 和 Y)和值(如 Z 表示高程)的点。不幸的是,如果我不把保存部分放进去,代码就可以工作,但是当我喜欢下面的时候,我得到了以下错误:

Traceback (most recent call last):
  File "C:\Users\Geri\Desktop\Sync\pythonlas\envisecond.py", line 33, in <module>
    listx1=p.append(listx1, float[newx])
AttributeError: 'float' object has no attribute 'append'
enter code here

到目前为止,我知道我需要一个数组来将值保存到代码末尾的 las 文件中:

import laspy
import laspy.file
import numpy as p
header = laspy.header.Header()
inFile2 = laspy.file.File("C:\\Users\\Geri\\Desktop\\Sync\\pythonlas\\mapperclip\\2clip.las", mode = "r")
inFile3 = laspy.file.File("C:\\Users\\Geri\\Desktop\\Sync\\pythonlas\\mapperclip\\3clip.las", mode = "r")
point_records = inFile2.points
point_records = inFile3.points

t=0
listx1=p.array([], dtype=float)
listy1=p.array([], dtype=float)
listz1=p.array([], dtype=float)
while t < 415:
    z=0
    q=0
    p=0.1
    while z==0:

        xmin=inFile3.x[t]-p
        ymin=inFile3.y[t]-p
        xmax=inFile3.x[t]+p
        ymax=inFile3.y[t]+p
        n=0
        for points in inFile2.points:
            ax=inFile2.x[n]
            ay=inFile2.y[n]
            if ax > xmin and ax < xmax and ay < ymax and ay > ymin: 

                newx = [inFile3.x[t]-((inFile3.x[t]-inFile2.x[n])/2)]
                newy = [inFile3.y[t]-((inFile3.y[t]-inFile2.y[n])/2)]
                newz = [inFile3.z[t]-((inFile3.z[t]-inFile2.z[n])/2)]
                listx1=p.append(listx1, float[newx])
                listy1=p.append(listy1, float[newy])
                listz1=p.append(listz1, float[newz])
                print listx1
                print n
                n+=1
                q+=1
                t+=1

            else:
                n+=1
        if q>0:            
            z+=1
        else:
            p+=0.1
outfile = laspy.file.File("C:\\Users\\Geri\\Desktop\\Sync\\pythonlas\\mapperclip\\output2.las", mode="w", header=header)
outfile.X = [listx1]
outfile.Y = [listy1]
outfile.Z = [listz1]
outfile.close() 

【问题讨论】:

  • 像大多数其他人一样使用import numpy as np

标签: python numpy lidar


【解决方案1】:

您在外部while 循环的开头将p 设置为float

p=0.1

这掩盖了顶部的 numpy 导入:

import numpy as p

所以在while 循环内p 不再是模块,它是一个float 对象并且调用p.append() 调用将失败。

为模块或 float 值使用不同的名称。

【讨论】:

  • 你是对的,我太傻了......遗憾的是,我遇到了一个新错误:Traceback(最近一次调用最后一次):文件“C:\用户\Geri\Desktop\Sync\pythonlas\ envisecond.py”,第 33 行,在 listx1=np.append(listx1, float[newx]) TypeError: 'type' object has no attribute 'getitem' >>>
  • @Gary - 这是因为您尝试使用 float[newx] 索引浮点类型。请改用float(newx)
  • 我取出了float类型,就这样使用了: listx1=np.append(listx1, (newx)) 代码运行,到达保存部分时才停止。我将把它移到另一个问题上。谢谢
猜你喜欢
  • 2017-12-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-05-14
  • 2016-04-07
  • 1970-01-01
  • 1970-01-01
  • 2018-05-21
相关资源
最近更新 更多