我正在使用Python脚本,该脚本旨在打印结构中的所有值以及值名称。结构中的值都是ctype,但是我在打印它们时遇到了麻烦。就目前而言,当我运行以下代码时
import ctypes
class test(ctypes.Structure):
pass
test._fields_ = [
('a', ctypes.c_float),
('b', ctypes.c_float),
('c', ctypes.c_float)]
d = test(1, 2, 3)
for field in d._fields_:
print field[0], field[1].value
我懂了
a <attribute 'value' of '_ctypes._SimpleCData' objects>
b <attribute 'value' of '_ctypes._SimpleCData' objects>
c <attribute 'value' of '_ctypes._SimpleCData' objects>
有什么想法吗?我以为.value应该从ctypes对象中获取值,但似乎并不想...
谢谢!
_fields_
具有字段名称和数据类型。您需要名称,并使用内置值getattr
:
>>> for name, dtype in d._fields_:
... print name, getattr(d, name)
...
a 1.0
b 2.0
c 3.0
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句