2012-08-01 97 views
0

Python正在调用C++函数(使用swig包装)。如何在python中使用std :: wstring

C++:

std::wstring getFilePathMultiByte(); 

我可以调用这个函数的蟒蛇。问题是我如何使用这个返回的wstring?想要将文件名追加到此路径中,如下面的输出中所示,这会产生错误。

的Python:

path = getFilePathMultiByte() 
    print path, type(path) 
    file = path + "/Information.log" 

输出:

_2012ad3900000000_p_std__wstring, type 'SwigPyObject' 
TypeError: unsupported operand type(s) for +: 'SwigPyObject' and 'str' 

如何在Python中创建一个std :: wstring的?这可能会让我连接。

谢谢。

+0

使用类型(路径)查看对象的可用方法。另外,'pprint.pprint(path)'会显示属性。 – jordanm 2012-08-01 20:15:28

回答

2

下面的例子担任意在与痛饮2.0我的机器上:

%module test 

%include "std_wstring.i" 

%inline %{ 
    std::wstring foo() { 
    return L"hi"; 
    } 
%} 

然后我用测试:

Python 2.7.3rc2 (default, Apr 22 2012, 22:30:17) 
[GCC 4.6.3] on linux2 
Type "help", "copyright", "credits" or "license" for more information. 
>>> import test 
>>> path = test.foo() 
>>> print path, type(path) 
hi <type 'unicode'> 
>>> file = path + "/Information.log" 
>>> print file 
hi/Information.log 
>>> 

我不太确定你做了什么错在这里 - 我的猜测是你没有得到%include "std_wstring.i",但是很难说出你所看到的东西。

+0

以为我会得到编译错误,如果适当的包括不存在。我有%包括“stl.i”。让我试试这个。顺便说一句 - 为你打印(路径)打印的是什么? – sambha 2012-08-01 22:25:38

+0

@sambha - 类型为“”,但我在原始答案中错误地标记了它,所以它被当作HTML处理!就SWIG而言,如果它不能充分了解类型以适当地包装它,它会假设类型是一个不透明的指针,并相应地生成一个包装。 – Flexo 2012-08-02 06:47:28

+0

谢谢。添加%包括“std_wstring.i”到正确的.i文件解决它。它的Unicode也是我的。我在字符串中有一些中文字符,所以打印由于其他原因而失败。在下一个。我也必须在我的问题中编辑。 – sambha 2012-08-02 14:49:50

相关问题