在python中形成包含整数数组、字符串列表的结构包

在python中形成包含整数数组、字符串列表的结构包,python,c,python-2.7,Python,C,Python 2.7,在C中,我们有 struct temp { unisgned int i[10]; char a[2][10]; }temp; 像这样,我用python制作了一个结构: integer_aray=[1,2,3,4,5] string_array=["hello","world"] format_="Qs" temp = namedtuple("temp","i a") temp_tuple = temp(i=

在C中,我们有

struct temp
{
  unisgned int i[10];
  char a[2][10];
}temp;
像这样,我用python制作了一个结构:

integer_aray=[1,2,3,4,5]
string_array=["hello","world"]
format_="Qs"
temp = namedtuple("temp","i a")                                      
temp_tuple =  temp(i=integer_array,a=string_array)
string_to_send = struct.pack(format_, *temp_tuple) 
当我像这样尝试Python2.7时,给出了一个错误

string_to_send = struct.pack(format_, *temp_tuple)
error: cannot convert argument to integer

我必须以整数数组和字符串数组的形式发送python结构。有没有办法不使用ctypes发送数组

char*a[2][10]是2x10指针的2D数组


你可能想做的是
chara[2][10]
是一个由2个C字符串组成的数组,每个字符串的长度为9+1个字符。或者可能是
char*a[2]
,这是指向字符(可能是数组)的两个指针。

如果您想打包C结构的等价物

struct temp
{
  unsigned int i[10];
  char a[2][10];
};
您将使用格式
“10I10s10s”
,其中
10I
表示10个本机顺序的4字节无符号整数,每个
10s
表示一个大小为10的字节字符串

在Python3中,您可以编写:

l = list(range(1, 11))            # [1,2,3,4,5,6,7,8,9,10]
temp = struct.pack("10I10s10s", *l, b"hello", b"world")
print(temp)
它将给出(在一个小小的endian ASCII平台上):


它与32位little endian系统上的C
temp
结构兼容。

是的,我需要字符a[2][10]我们不能以字符串列表的形式发送。或者以任何其他方式使用ctypes模块或任何其他模块。
b'\x01\x00\x00\x00\x02\x00\x00\x00\x03\x00\x00\x00\x04\x00\x00\x00\x05\x00\x00\x00\x06\x00\x00\x00\x07\x00\x00\x00\x08\x00\x00\x00\t\x00\x00\x00\n\x00\x00\x00hello\x00\x00\x00\x00\x00world\x00\x00\x00\x00\x00'