23maverick23/oaxmlapi

TypeError: Can't convert 'bytes' object to str implicitly

pkchoksi opened this issue · 4 comments

First of all thank you for this open source library, but it seems that I am getting this error and perfectly following the documentation. I am using python 3. I am getting the error on Connections.Request() method.
screenshot 72

screenshot 73

xml_req = connections.Request( app, auth, xml_array).request().tostring() this does not work either as it throws this error AttributeError: 'xml.etree.ElementTree.Element' object has no attribute 'tostring'

Hey @pkchoksi - thanks for using the package. I haven't tested this with Python 3, which may be the reason you are getting this error (since PY3 deals with unicode strings and bytestrings differently). I'm pretty sure there would need to be some refactoring to add full support for PY3.

If you look at the PY2 docs, you'll see that you can set the encoding of an ET element specifically to UTF-8 which is what I am doing:
https://docs.python.org/2/library/xml.etree.elementtree.html#xml.etree.ElementTree.tostring

In the PY3 docs however, it looks like you may need to set it to encoding="unicode" otherwise the system thinks you're using a bytestring (and that leads to the output of your stack trace):
https://docs.python.org/3/library/xml.etree.elementtree.html#xml.etree.ElementTree.tostring

If you have time to write up a patch for PY3 support, please do. Otherwise I can put this in my backlog and take a look at it soon. My guess is I'd need to review all the source files for proper PY3 conversion, so it might be a bigger project than this one fix.

thanks for the response. I am no longer using urllib to request data. I am instead using requests which actually solved the problem. The req will have to be changed into a string using xml element tree and encoding it to unicode and set the request type to xml and it works perfectly.

Great - thanks for letting me know. Good luck!