Python3 error: initial_value must be str or None, with StringIO
Solution 1:
response.read()
returns an instance of bytes
while StringIO
is an in-memory stream for text only. Use BytesIO
instead.
From What's new in Python 3.0 - Text Vs. Data Instead Of Unicode Vs. 8-bit
The
StringIO
andcStringIO
modules are gone. Instead, import theio
module and useio.StringIO
orio.BytesIO
for text and data respectively.
Solution 2:
This looks like another python3 bytes
vs. str
problem. Your response is of type bytes
(which is different in python 3 from str
). You need to get it into a string first using response.read().decode('utf-8')
say and then use StringIO
on it. Or you may want to use BytesIO
as someone said - but if you expect it to be str
, preferred way is to decode
into an str
first.
Solution 3:
Consider using six.StringIO instead of io.StringIO.