pythonc的结构体
❶ python中定义的结构体问题: 类似c语言中的如下这种形式 typedef struct { int x; int y; int h; }point;
classblock():
def__init__(self):
self.x=0
self.y=0
self.z=0
point=[block()foriinrange(100)]
❷ Python中如何使用C的结构体struct求解
閟truct就可以使用结构体了:
import struct
生成一个结构体实例:
data = struct.pack( 'format_string', struct_menber_1, struct_menber_2, ... )
其中的format_string用来指定结构体的格式(指明该结构体在C中的定义),由两部分组成:
首先是一个可选的特殊字符,用来指明字节序、数据类型大小和对齐方式:
@: native order, size & alignment (default)
=: native order, std. size & alignment
<: little-endian, std. size & alignment
>: big-endian, std. size & alignment
!: same as >
然后是指明结构体定义的部分:
The remaining chars indicate types of args and must match exactly;
these can be preceded by a decimal repeat count:
x: pad byte (no data); c:char; b:signed byte; B:unsigned byte;
h:short; H:unsigned short; i:int; I:unsigned int;
l:long; L:unsigned long; f:float; d:double.
Special cases (preceding decimal count indicates length):
s:string (array of char); p: pascal string (with count byte).
Special case (only available in native format):
P:an integer type that is wide enough to hold a pointer.
Special case (not in native mode unless 'long long' in platform C):
q:long long; Q:unsigned long long
Whitespace between formats is ignored.
如果struct模块的函数出错,将产生struct.error异常。
❸ 各位老板,python怎么把c里面的结构体读进来
展开全部
閟truct就可以使用结构体了:
import
struct
生成一个结构体实例:
data
=
struct.pack(
'format_string',
struct_menber_1,
struct_menber_2,
...
)
其中的format_string用来指定结构体的格式(指明该结构体在C中的定义),由两部分组成
❹ PYTHON怎么实现类似C里的结构体数组
fromctypesimport*
classAAA(Structure):
_fields_=[("a",c_int),
("b",c_int),
("c",c_int),
("d",c_int),
]
❺ python里面可以定义结构体吗
Python中没有专门定义结构体的方法,但可以使用class标记定义类来代替结构体,
其成员可以在构造函数__init__中定义,具体方法如下。
复制代码代码如下:
class item:
def __init__(self):
self.name = '' # 名称
self.size = 10 # 尺寸
self.list = [] # 列表
a = item() # 定义结构对象
a.name = 'cup'
a.size = 8
a.list.append('water')
❻ C++ 如何将定义的结构体传到python里
用 ctypes, 接收返回的对象建个类, 类似下面这种:
from ctypes import *class StandGo(Structure): _fields_ = [("x", c_int),("y", c_int)] 接收对象赋值给这个类应该就可以了 具体的看ctypes的文档了,自己试下了
❼ Python向怎么向C语言传递结构体
况如下:
打算从Python发一个TCP数据包给远程服务器,数据的主体是一个C语言的 struct (较大,size 为1402)。由于这个struct太复杂,故不打算在python 处对其重新定义,目前的想法是用python调用一个C语言的模块,在这个模块中定义这个Struct,并设置好数据后,将其struct传回python中,再打包传送服务器。
但是不知道如何将这个struct 变量从C语言 传入Python中。尝试用Py_BuildValue函数,以Py_BuildValue("P",&interface_setup) //interface_setup为结构体变量
传递,
但是几次都得到运行时错误:
SystemError: bad format char passed to PyBuildVaule。
❽ Python中有结构体吗
结构体没有,你可以试试字典,要么可以写个类。
❾ Python 如何给 c 函数传递结构体参数
//test1.c#include<stdio.h>#include<stdlib.h>structStudent
{charname[30];floatfScore[3];
};voidDisplay(structStudentsu){printf("-----Information------ ");printf("Name:%s",su.name);printf("Chinese:%.2f ",su.fScore[0]);printf("Math:%.2f ",su.fScore[1]);printf("English:%.2f",su.fScore[2]);printf("平均分数为:%.2f ",(su.fScore[0]+su.fScore[1],su.fScore[2])/3);
}
❿ 如何在Python中使用C/C++结构体等复杂类型
C封装了一个Dll,名为SpjMatlabTest.dll,暴露实现:
typedef struct{
double *Min;
double *Max;
} ST_TESTLIMIT;
typedef struct{
int NumVDIVs;
int NumPts;
double *TestedVDIVs; // Pointer to first element in array that is 1xNumVDIVs long.
double *TestPtsinFracFS; // Pointer to first element in array that is 1xNumPts long.
double *VerrinFracFS; // Pointer to first element in array that is NumVDIVs*NumPts*NumChans long.
double DCAcc;
double OffConstAcc;
double *GainErrorPct; // Pointer to first element in array of NumVDIVs*NumChans long.
double *OffsetErrorFracFS; // Pointer to first element in array of NumVDIVs*NumChans long.
double *MaxNonLinearityFracFS; // Pointer to first element in array of NumVDIVs*NumChans long.
double GainErrorLimitPctAbs;
double TDIV;
} ST_LINACCD1M;
#include <windows.h>
#ifndef BUILD_DLL
//Define the function prototypes to be used by the calling application.
typedef bool (__cdecl *MATLABAPPINITIALIZE)(void);
typedef bool (__cdecl *MATLABAPPTERMINATE)(void);
typedef bool (__cdecl *LINACCD1M)(ST_LINACCD1M *stOutput, const bool *Channels, const char *ScopeAddress, const int GPIBAddress);
typedef void (__cdecl *LINACCD1MCLEANUP)(void);
#endif
现在需要在py代码中使用ST_LINACCD1M结构体。
测试代码:
from ctypes import *
class C_ST_LINACCD1M(Structure):
_fields_ = [('NumVDIVs', c_int),
('NumPts', c_int),
('TestedVDIVs', c_void_p),
('TestPtsinFracFS', c_void_p),
('VerrinFracFS', c_void_p),
('DCAcc', c_double),
('OffConstAcc', c_double),
('GainErrorPct', c_void_p),
('OffsetErrorFracFS', c_void_p),
('MaxNonLinearityFracFS', c_void_p),
('GainErrorLimitPctAbs', c_double),
('TDIV', c_double)]
import sys
def visit():
info = C_ST_LINACCD1M()
#print info.sth...
dllName = "SpjMatlabTest.dll"
dllload = windll.LoadLibrary(dllName)
print dllload
py_LINACCD1M = dllload.LINACCD1M
py_LINACCD1M.argtypes = [c_void_p, c_void_p, c_char_p, c_int]
#.restypep = c_void_p
para_in_1 = True
para_in_2 = c_char_p("/0")
para_in_3 = 0
nRetVal = dllload.LINACCD1M(byref(info), para_in_1, para_in_2, para_in_3)
#print info.sth...
if __name__ == "__main__":
visit()
打完收工。