ofxclient
ofxclient copied to clipboard
Unicode vs. bytes in Python 3.x
I'm getting a TypeError when I try to list the accounts for my institution:
*** Python 3.4.5 |Anaconda 2.1.0 (64-bit)| (default, Jul 5 2016, 14:53:07) [MSC v.1600 64 bit (AMD64)] on win32. ***
>>> institution.accounts()
Traceback (most recent call last):
File "<string>", line 301, in runcode
File "<interactive input>", line 1, in <module>
File "C:\Anaconda3\lib\site-packages\ofxclient\institution.py", line 143, in accounts
parsed = OfxParser.parse(resp_handle)
File "C:\Anaconda3\lib\site-packages\ofxparse\ofxparse.py", line 398, in parse
ofx_file = OfxPreprocessedFile(file_handle)
File "C:\Anaconda3\lib\site-packages\ofxparse\ofxparse.py", line 168, in __init__
super(OfxPreprocessedFile, self).__init__(fh)
File "C:\Anaconda3\lib\site-packages\ofxparse\ofxparse.py", line 98, in __init__
self.read_headers()
File "C:\Anaconda3\lib\site-packages\ofxparse\ofxparse.py", line 104, in read_headers
head_data = head_data[:head_data.find(six.b('<'))]
TypeError: Can't convert 'bytes' object to str implicitly
I've ran into the same issue. Now trying in Python 2.7.
Ran into this as well. Removing the call to six.b()
fixed it, but kept tripping over further conversion/encoding errors.
The issue is that ofxparse expects a "seekable file-like byte stream object".
See line 87, https://github.com/jseutter/ofxparse/blob/master/ofxparse/ofxparse.py
Ofxclient is providing a StringIO object to ofxparse [StringIO.StringIO for Python 2, IO.StringIO for Python3].
Ofxparse is apparently happy with the Python 2 StringIO.StringIO object, but is not happy with the Python3 IO.StringIO.
However converting the IO.StringIO object to an IO.BytesIO object fixes the issue.
For example, account.py, line 112:
change:
return OfxParser.parse(self.download(days=days))
to:
return OfxParser.parse(BytesIO(((self.download(days=days)).read()).encode()))
seems to work for Python3, assuming IO.BytesIO is imported.
I'll fork and request pull in the next few days.
Pull request #30 submitted to resolve this issue.