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