How can I resolve TypeError with StringIO in Python 2.7? How can I resolve TypeError with StringIO in Python 2.7? python python

How can I resolve TypeError with StringIO in Python 2.7?


You can resolve the error by simply adding a u before your string to make the string unicode:

datastring = StringIO(u"""\Country  Metric           2011   2012   2013  2014USA     GDP               7      4     0      2USA     Pop.              2      3     0      3GB      GDP               8      7     0      7GB      Pop.              2      6     0      0FR      GDP               5      0     0      1FR      Pop.              1      1     0      5""")

Your initial value should be unicode.


Rather use (fixes this exact issue for me):

from StringIO import StringIO