我的问题是如何通过 ctypes 将 C 函数的制表符分隔输出解析为 pandas DataFrame:
我正在使用 ctypes 在 Python3.x 中围绕 C 库编写一个 Python 包装器。 C 库当前执行数据库查询。我正在访问的C函数return_query()
给定文件路径、索引和查询字符串,从查询中返回制表符分隔的行:
int return_query(structname **output, const char *input_file,
const char *index, const char *query_string);
正如你所看到的,我正在使用output
作为存储查询中所有记录的位置,其中structname
是行的结构
我还有一个打印到 STDOUT 的函数:
int print_query(const char *input_file,
const char *index, const char *query_string);
我的目标是通过 ctypes 访问这些函数,并将制表符分隔的行输出传递到 pandas DataFrame 中。
我的问题是这样的:
(1) 我可以尝试解析 STDOUTprint_query()
;但是,这些查询可能会产生大型制表符分隔的 DataFrame。我担心这个解决方案效率不高,因为它可能无法扩展到+10000 行。其他问题大致涵盖了如何通过 ctypes 从 Python 中的 C 函数捕获 STDOUT:
使用 ctypes 模块从 python 调用的共享库捕获打印输出 https://stackoverflow.com/questions/9488560/capturing-print-output-from-shared-library-called-from-python-with-ctypes-module
(2) 我可以访问吗output
以某种方式,并将其传递给 pandas DataFrame?我目前不确定这将如何工作,例如
import ctypes
lib = CDLL("../libshared.so") ### reference to shared library, *.so
lib.return_query.restype = ctypes.c_char
lib.return_query.argtypes = (???, ctypes.c_char_p, ctypes.c_char_p, ctypes.c_char_p)
第一个参数应该是什么,我如何将它传递给可能是 pandas DataFrame 的东西?
(3) 也许最好将返回制表符分隔行的 C 函数重写为更容易通过 ctypes 访问的内容?