Python,负数字符串浮点数

问题描述 投票:2回答:5

这是一个非常简单的问题,但我不必要地使其变得复杂,并继续遇到障碍。

我正在尝试解析一个简单的文本文件,其中包含点云信息x,y,z。

看起来像这样:

vertices:
v -543.243 -494.262 1282
v -538.79 -494.262 1282
v -536.422 -496.19 1287
v -531.951 -496.19 1287
v -527.481 -496.19 1287
v -213.909 -223.999 581
v -212.255 -224.384 582
v -209.15 -223.228 579
v -207.855 -223.999 581
v -205.482 -223.613 580
v -203.468 -223.613 580
v -201.106 -223.228 579
v -199.439 -223.613 580
v -197.765 -223.999 581
v -195.41 -223.613 580
v -193.062 -223.228 579
v -190.721 -222.842 578
v -189.04 -223.228 579
v -187.998 -224.384 582
v -185.976 -224.384 582
v -183.955 -224.384 582
v -181.621 -223.999 581
v -179.293 -223.613 580
v -177.279 -223.613 580
v -175.264 -223.613 580
v -173.549 -223.999 581
v -171.531 -223.999 581
v -169.513 -223.999 581
v -167.495 -223.999 581
v -165.761 -224.384 582
v -163.74 -224.384 582
v -161.718 -224.384 582
v -159.697 -224.384 582
v -157.946 -224.77 583
v -155.921 -224.77 583
v -153.896 -224.77 583
v -151.871 -224.77 583
v -149.847 -224.77 583
v -147.568 -224.384 582

好吧。还不错

接下来,我在搅拌器中使用python将这些点转换为顶点:

看起来像这样:`

    #Open the file
try:
    f=open(path, 'r')
except:
    print ("Path is not Valid")


#Create an array of 
verts = []
#verts=[float(e) for e in verts if e]
#values = []

for line in f:
    lines = f.readline() 
    #values = ([c for c in lines if c in '-1234567890.'])
    if line.startswith("v "): #Go through file line by line
        read = lines.strip(' v\n').split(',') #remove the v,split@, 
            #read = values.split(',')
        #loop over all stuff in read, remove non-numerics
        for d in read:
            d= d.strip('-').split(' ')
            print("d:", (d))
            for n in d:
                n = n.strip('-')
                verts = verts.append(float(n[0]))
                #verts = verts.append(float(n[2]))
                #verts = verts.append(float(n[3]))
                print("vertsloops", d[0])
            print("read1", read[0])
            print(read)
            print("oo1verts", verts)
             ##################
             #Extend the array# 
             #print ("Could not use the line reading: %s"%read)


# Create a new mesh, it is now empty
mesh = bpy.data.meshes.new("Cube")
# Create empty vertices field in the mesh
mesh.vertices.add(len(verts))
# Add vertices
mesh.vertices.foreach_set("co", verts)


#Add a new empty object named "Read the PointCloud Data"
obj = bpy.data.objects.new("Read the PointCloud Data", mesh)
# Link object to current scene
bpy.context.scene.objects.link(obj)
`

以某种方式,我尝试了许多不同的分割字符串的组合,但是仍然会出错。我知道这是一个简单的任务!任何建议,请!

我首先看到的错误是:

d: ['-536.422', '-496.19', '1287']
Traceback (most recent call last):
  File "/Users/.../importpoints.blend/importpoints", line 37, in <module>
ValueError: could not convert string to float: '-'
Error: Python script fail, look in the console for now...

然后像这样:

d: ['536.422', '-496.19', '1287']
vertsloops 536.422
Traceback (most recent call last):
  File "/Users/.../importpoints.blend/importpoints", line 37, in <module>
AttributeError: 'NoneType' object has no attribute 'append'
Error: Python script fail, look in the console for now...

一件事是,如何将float('-531')视为负数。就目前而言,它会击中“-”号,并认为它是非数字的,因此无法将其转换。但是它是负面的,那么我该如何指出呢?

谢谢。

这是一个非常简单的问题,但我不必要地使其变得复杂,并继续遇到障碍。我正在尝试解析一个简单的文本文件,其中包含点云信息x,y,z。看起来...

python python-2.7 point-clouds blender-2.67
5个回答
2
投票

使您的阅读短一些:


1
投票

转换为浮动负数的字符串没有问题


0
投票

谢谢,这很有帮助。如果有人需要,下面是最终代码。


0
投票

所以只是为了添加答案...


0
投票

对于其他人,遇到了这个问题。这也是由于使用非ASCII字符表示“-”。只需使用正则表达式或其他方法将非ascii字符固定在float表示中,即可进行转换。有关更多详细信息,请访问Python convert string to float error with negative numbers

© www.soinside.com 2019 - 2024. All rights reserved.