Ich versuche, zurückgegebene Zeichenfolge Werte aus einer Python-Funktion in C++ mit Boost Python.Boost :: Python-Extrakt - Zugreifen auf Python-Zeichenfolge in C++
Ich sehe diesen Fehler:
TypeError: No registered converter was able to produce a C++ rvalue of type std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> > from this Python object of type function
Der Fehler Sinn macht, ist der Python-Datentyp der C++ Datentypen nicht übereinstimmen. Aber ich bin mir nicht sicher, wie ich das beheben soll.
Der C++ Code ist:
Py_Initialize();
try {
boost::python::object main = boost::python::import("__main__");
boost::python::object global(main.attr("__dict__"));
boost::python::object result = exec_file("/Users/tomconlin/Documents/495_496_Research/Current_Source_Code/Source_Files/test_hello.py", global, global);
boost::python::object test_hello = global["get_info"];
std::string message = boost::python::extract<std::string>(test_hello);
std::string m = message;
std::cout << message << std::endl;
return message;
}
catch (boost::python::error_already_set&) {
PyErr_Print();
}
Py_Finalize();
std::string error_string = "\nAn Error Occured\n";
return error_string;
Der Python-Code ist:
import sys
sys.path.append("/Users/tomconlin/anaconda/lib/python2.7/site-packages")
def get_info():
version = 2 # API version
block_io = BlockIo('2c18-7990-8d0d-7511', '7816902248', version)
credit_card_balance = block_io.get_address_balance(addresses='2Mytv6K7BkiBRuHhnHmVTTc51xyhLRNtDgr')
#print "Credit Card Balance Type:", type(credit_card_balance)
address = ""
for key in credit_card_balance:
address += str(credit_card_balance[key])
#print "This is the address:", address
address = address.split()
#print address
print "\n\n"
return_address = address[12].strip("u',")
print return_address
print "\n\n"
return return_address
def main():
get_info()
if __name__ == '__main__':
from block_io import BlockIo
main()
Sie waren völlig richtig. Mit dem Code wurde die Funktion namens "test_hello" extrahiert, anstatt die Stringvariable zu extrahieren, die ich gesucht habe, nämlich "return_address". Ein Teil meiner Arbeit dafür bestand darin, den Python-Code ohne die Funktionen 'get_info()' und 'main()' auszuführen, was natürlich nicht ideal ist, aber mir erlaubte, den Wert 'return_address' durch' boost' zu übernehmen :: python :: object test_hello = global ["return_address"]; ', dann extrahieren. – TMC