如何在python中使用std :: wstring

时间:2012-08-01 20:11:31

标签: python internationalization swig std

Python正在调用C ++函数(使用swig包装)。

C ++:

  std::wstring getFilePathMultiByte();

我可以在python中调用这个函数。问题是如何使用此返回的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?这可能允许我连接。

感谢。

1 个答案:

答案 0 :(得分:2)

以下示例在我的计算机上按预期使用SWIG 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",但是鉴于你所展示的内容很难肯定。