简体   繁体   English

读取.obj文件和分割线

[英]Reading .obj file and split lines

I made a simple code to read a .obj file that looks like this 我编写了一个简单的代码来读取看起来像这样的.obj文件

g cube

v  0.0  0.0  0.0
v  0.0  0.0  1.0
v  0.0  1.0  0.0
v  0.0  1.0  1.0
v  1.0  0.0  0.0
v  1.0  0.0  1.0
v  1.0  1.0  0.0
v  1.0  1.0  1.0

f  1//2  7//2  5//2
f  1//2  3//2  7//2 
f  1//6  4//6  3//6 
f  1//6  2//6  4//6 
f  3//3  8//3  7//3 
f  3//3  4//3  8//3 
f  5//5  7//5  8//5 
f  5//5  8//5  6//5 
f  1//4  5//4  6//4 
f  1//4  6//4  2//4 
f  2//1  6//1  8//1 
f  2//1  8//1  4//1 

And the python code looks like this python代码看起来像这样

class objeto():
def __init__(self, obj = None):
    if obj:
        self.cargar_obj(obj)

def cargar_obj(self, archivo):
    with open(archivo, 'r') as obj:
        datos = obj.read()

    lineas = datos.splitlines()
    self.vertices = []
    self.superficies = []

    for linea in lineas:
        elem = linea.split()
        if elem:
            if elem[0] == 'v':
                v = vertice(float(elem[1]), float(elem[2]), float(elem[3]))
                self.vertices.append(v)
            elif elem[0] == 'f':
                vs = []
                for i in range(1, len(elem)):
                    vs.append(self.vertices[int(elem[i].split('/'))])

                f = cara(vs)
                self.superficies.append(f)
            else:
                pass

The problem appears to be this line: 问题似乎是此行:

vs.append(self.vertices[int(elem[i].split('/'))])

because when I try to run the code, the followed TypeError appears 因为当我尝试运行代码时,出现以下TypeError

int() argument must be a string, a bytes-like object or a number, not 'list'

I'm a newbie in Python, so I don't seem to get around this error, can somebody give me some advice? 我是Python的新手,因此似乎无法解决此错误,有人可以给我一些建议吗? Thanks. 谢谢。

I'm using Python 3.x, and ipython to run .py files 我正在使用Python 3.x和ipython运行.py文件

Recreate your case with one line: 用一行重新创建您的案例:

In [435]: line='f  1//2  7//2  5//2'
In [436]: elem = line.split()
In [437]: elem
Out[437]: ['f', '1//2', '7//2', '5//2']
In [438]: elem[0]
Out[438]: 'f'

split on / behaves as I expect: 分割/行为符合我的预期:

In [439]: for i in range(1,len(elem)):
     ...:    print(elem[i].split('/'))
     ...:    
['1', '', '2']
['7', '', '2']
['5', '', '2']

Your code has problems applying int to that list of strings: 您的代码在将int应用于该字符串列表时遇到问题:

In [440]: for i in range(1,len(elem)):
     ...:    print(int(elem[i].split('/')))
     ...
TypeError: int() argument must be a string, a bytes-like object or a number, not 'list'

So we need to apply int() to the individual strings, not the list. 因此,我们需要将int()应用于单个字符串,而不是列表。 But now the empty string is giving me problems. 但是现在空字符串给我带来了问题。

In [441]: for i in range(1,len(elem)):
     ...:    print([int(e) for e in elem[i].split('/')])
     ...:        
ValueError: invalid literal for int() with base 10: ''

The rest is left as an exercise for the reader. 剩下的作为练习留给读者。

Couldn't resist: 无法抗拒:

[int(e) for e in elem[i].replace('//','/').split('/')]
[int(e) for e in elem[i].split('/') if e]

The error message is pretty clear: you are passing int a list (the result of calling split ); 错误消息非常清楚:您正在向int传递一个列表(调用split的结果); maybe you only meant to call it with one of the elements of that list. 也许您只是想用该列表的元素之一来调用它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM