[英]Python: Can we convert a ctypes structure to a dictionary?
I have a ctypes structure. 我有一个ctypes结构。
class S1 (ctypes.Structure):
_fields_ = [
('A', ctypes.c_uint16 * 10),
('B', ctypes.c_uint32),
('C', ctypes.c_uint32) ]
if I have X=S1(), I would like to return a dictionary out of this object: Example, if I do something like: Y = X.getdict() or Y = getdict(X), then Y might look like: 如果我有X = S1(),我想从这个对象中返回一个字典:例如,如果我做了类似的事情:Y = X.getdict()或Y = getdict(X),那么Y可能看起来像:
{ 'A': [1,2,3,4,5,6,7,8,9,0],
'B': 56,
'C': 8986 }
Any help ? 有帮助吗?
Probably something like this: 可能是这样的:
def getdict(struct):
return dict((field, getattr(struct, field)) for field, _ in struct._fields_)
>>> x = S1()
>>> getdict(x)
{'A': <__main__.c_ushort_Array_10 object at 0x100490680>, 'C': 0L, 'B': 0L}
As you can see, it works with numbers but it doesn't work as nicely with arrays -- you will have to take care of converting arrays to lists yourself. 正如您所看到的,它适用于数字,但它不能很好地与数组一起使用 - 您必须自己负责将数组转换为列表。 A more sophisticated version that tries to convert arrays is as follows: 尝试转换数组的更复杂版本如下:
def getdict(struct):
result = {}
for field, _ in struct._fields_:
value = getattr(struct, field)
# if the type is not a primitive and it evaluates to False ...
if (type(value) not in [int, long, float, bool]) and not bool(value):
# it's a null pointer
value = None
elif hasattr(value, "_length_") and hasattr(value, "_type_"):
# Probably an array
value = list(value)
elif hasattr(value, "_fields_"):
# Probably another struct
value = getdict(value)
result[field] = value
return result
If you have numpy
and want to be able to handle multidimensional C arrays, you should add import numpy as np
and change: 如果你有numpy
并希望能够处理多维C数组,你应该将import numpy as np
添加import numpy as np
并更改:
value = list(value)
to: 至:
value = np.ctypeslib.as_array(value).tolist()
This will give you a nested list. 这将为您提供嵌套列表。
How about something like: 怎么样的:
class S1(ctypes.Structure):
_fields_ = [ ... ]
def getdict(self):
dict((f, getattr(self, f)) for f, _ in self._fields_)
A little bit more general purpose to handle double arrays, and arrays of structures, and bitfields. 处理双数组,结构数组和位域的一般目的。
def getdict(struct):
result = {}
#print struct
def get_value(value):
if (type(value) not in [int, long, float, bool]) and not bool(value):
# it's a null pointer
value = None
elif hasattr(value, "_length_") and hasattr(value, "_type_"):
# Probably an array
#print value
value = get_array(value)
elif hasattr(value, "_fields_"):
# Probably another struct
value = getdict(value)
return value
def get_array(array):
ar = []
for value in array:
value = get_value(value)
ar.append(value)
return ar
for f in struct._fields_:
field = f[0]
value = getattr(struct, field)
# if the type is not a primitive and it evaluates to False ...
value = get_value(value)
result[field] = value
return result
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.