【问题标题】:Extract and convert list element of boost::python::list提取和转换 boost::python::list 的列表元素
【发布时间】:2014-11-14 01:28:47
【问题描述】:

我有intstring 的异构列表,我想将它们全部存储在vector<string> 中。 使用此命令:

    std::string temp = boost::python::extract<std::string>(xList[i][j]);

我收到此错误:

TypeError: No registered converter was able to produce a C++ rvalue of type std::string from this Python object of type float

【问题讨论】:

    标签: python c++ boost-python


    【解决方案1】:

    你有两个选择:要么获取值作为boost::python::object 并检查类型并做任何你喜欢的事情,要么register a converter 将数字转换为字符串(大概使用std::to_string)。

    您可以使用docs 中“提取 C++ 类型”的说明:

    extract<std::string&> extractor(xList[i][j]);
    if (extractor.check()) {
        std::string& v = extractor();
    

    【讨论】:

    • 谢谢,我怎样才能得到boost::python::object 并检查类型?
    • 为什么字符串被定义为引用?
    猜你喜欢
    • 2016-12-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-02-25
    • 2017-03-12
    • 1970-01-01
    • 1970-01-01
    • 2019-06-28
    相关资源
    最近更新 更多