参考:TypeError: unicode argument expected, got 'str'

Python代码:

from io import StringIO

def main():
f = StringIO()
f.write('Hi')
f.write(' ')
f.write('all')
···

解释器报错:

Traceback (most recent call last):
File "./stringio.py", line 19, in <module>
main()
File "./stringio.py", line 7, in main
f.write(str('Hi'))
TypeError: unicode argument expected, got 'str'

stackoverflow上对这个问题的解释是:

io.StringIO is confusing in Python 2.7 because it's backported from the 3.x bytes/string world.

backported: 名词解释

意思就是新版本的python3直接往这个库中加入了一些新的内容,使得该库在Python2.7中较为混乱。

解决方法是将导入语句更换为:

from io import BytesIO as StringIO

2017.3.15

最新文章

  1. [转]Linux中configure/makefile
  2. Docker知识-1
  3. 偶然翻出很久很久以前写的一款sqlmap UI,有点年头了
  4. 从零开始学CSRF
  5. Django函数——url()
  6. HTML4.01和XHTML1.0和XHTML1.1的一些区别
  7. iOSbase64
  8. 3月3日(2) Search Insert Position
  9. hdoj 1384 Intervals
  10. 5.6.3.4 trim()方法
  11. opencv之haar特征+AdaBoos分类器算法流程(二)
  12. iOS国际化和genstrings所有子文件夹本地化字符串
  13. 排序算法的C#实现
  14. appium初学者,使用之检查appium环境报错Could not detect Mac OS X Version from sw_vers output: &#39;10.12.1’,
  15. 请求库-selenium 模块
  16. mysql GTID
  17. 指针运算中的运算符:&amp;和*
  18. Spring P命名空间 02
  19. C#:导入Excel通用类(Xls格式)
  20. net mvc中angular

热门文章

  1. SenchaTouch调用纯数字键盘
  2. my sql 两个 索引 时的 union 与 or 的比较
  3. URLSearchParams 接口定义处理 URL 参数串
  4. chkconfig --add失败的处理方法
  5. 【BZOJ3275】Number 最小割
  6. oracle的分页查询,mabatis的sql配置
  7. Swift - 获取状态栏一些信息
  8. 再谈js的作用域
  9. 3.html+.ashx(删除学生信息)
  10. SaltStack系列(三)之state相关介绍