How can I resolve TypeError with StringIO in Python 2.7?

痴心易碎 提交于 2019-11-28 09:40:45

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  2014
USA     GDP               7      4     0      2
USA     Pop.              2      3     0      3
GB      GDP               8      7     0      7
GB      Pop.              2      6     0      0
FR      GDP               5      0     0      1
FR      Pop.              1      1     0      5
""")

Your initial value should be unicode.

Rather use (fixes this exact issue for me):

from StringIO import StringIO
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!