0

Python は C++ 関数を呼び出しています (swig を使用してラップされています)。

C++:

  std::wstring getFilePathMultiByte();

この関数は Python で呼び出すことができます。質問は、この返された wstring をどのように使用するかです。以下の出力に示すように、このパスにファイル名を追加するとエラーが発生します。

パイソン:

  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を作成するにはどうすればよいですか? これにより、連結が可能になる場合があります。

ありがとう。

4

1 に答える 1

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"、あなたが示したことを考えると、確かに言うのは難しいです.

于 2012-08-01T21:01:35.020 に答える