如何在python中解析C数组

如何在python中解析C数组,python,parsing,hexdump,Python,Parsing,Hexdump,我有一个包含以下C数组的文件: /* Frame bytes */ static const unsigned char pkt1[19] = { 0x83, 0x01, 0xbc, 0x4e, 0xd9, 0x09, 0x81, 0x03, /* ...N.... */ 0x79, 0x54, 0x55, 0x98, 0x69, 0x06, 0x0a, 0x12, /* yTU.i... */ 0x42, 0x83, 0x62 /*

我有一个包含以下C数组的文件:

/* Frame bytes */
static const unsigned char pkt1[19] = {
0x83, 0x01, 0xbc, 0x4e, 0xd9, 0x09, 0x81, 0x03, /* ...N.... */
0x79, 0x54, 0x55, 0x98, 0x69, 0x06, 0x0a, 0x12, /* yTU.i... */
0x42, 0x83, 0x62                                /* B.b */
};

/* Frame bytes */
static const unsigned char pkt2[11] = {
0x83, 0x01, 0xbc, 0x4e, 0x39, 0x09, 0x81, 0x03, /* ...N9... */
0x52, 0x80, 0x30                                /* R.0 */
};

/* Frame (115 bytes) */
static const unsigned char pkt3[27] = {
0x83, 0x01, 0xbc, 0x4e, 0x39, 0x09, 0x81, 0x03, /* ...N9... */
0x01, 0x00, 0x6c, 0x1f, 0xa2, 0x1d, 0x02, 0x01, /* ..l..... */
0x04, 0x08, 0x34, 0x12, 0x21, 0x00, 0x35, 0x63, /* ..4.!.5c */
0x92, 0x91, 0x65                                /* ..e */
};
python中有没有任何简单的方法来解析这个文件并提取十六进制流?我需要这样的输出:

0x83, 0x01, 0xbc, 0x4e, 0xd9, 0x09, 0x81, 0x03, 0x79, 0x54, 0x55, 0x98, 0x69, 0x06, 0x0a, 0x12, 0x42, 0x83, 0x62

0x83, 0x01, 0xbc, 0x4e, 0x39, 0x09, 0x81, 0x03, 0x52, 0x80, 0x30

0x83, 0x01, 0xbc, 0x4e, 0x39, 0x09, 0x81, 0x03, 0x01, 0x00, 0x6c, 0x1f, 0xa2, 0x1d, 0x02, 0x01, 0x04, 0x08, 0x34, 0x12, 0x21, 0x00, 0x35, 0x63, 0x92, 0x91, 0x65
或:

那就:

[re.sub("/\*.+\*/", "", m).replace('\n', '').strip() for m in re.findall("{(.+?)};", c_file_as_string, re.S)]

通常,您必须用Python编写一个C解析器。非常困难的事情。但是如果文件像我们看到的那样简单,那么您可以简单地搜索
{…}之间的所有内容通过正则表达式。@我的第一个想法也是奇怪地搜索
{…}
块。只是想看看python中是否有能够理解C数组的库。似乎在Vim中没有。
:%s,/.*/,
:g/{/norm jvi}J
:g/[{}]/d
[re.sub("/\*.+\*/", "", m).replace('\n', '').strip() for m in re.findall("{(.+?)};", c_file_as_string, re.S)]