参考: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

Python - TypeError: unicode argument expected, got 'str'的更多相关文章

  1. Python2.7 在使用BSTestRunner.py时报错TypeError: unicode argument expected, got 'str'

    python3往这个库中加入了一些新的内容,使得该库在Python2.7中报错. 解决方法是将导入语句 from io import StringIO as StringIO 更换为: from io ...

  2. Python错误TypeError: write() argument must be str, not bytes

    2016-07-03 20:51:25 今天使用Python中的pickle存储的时候出现了以下错误: TypeError: write() argument must be str, not byt ...

  3. Python 读写文件 中文乱码 错误TypeError: write() argument must be str, not bytes+

    今天写上传文件代码,如下 def uploadHandle(request): pic1=request.FILES['pic1'] picName=os.path.join(settings.MED ...

  4. moviepy音视频剪辑:headblur函数遇到的TypeError: integer argument expected, got float错误的解决方案

    运行环境如下: python版本:3.7 opencv-python版本:4.2.0.34 numpy版本:1.19.0 错误信息: 在调用moviepy1.03版本的headblur函数执行人脸跟踪 ...

  5. python3 pycurl 出现 TypeError: string argument expected, got 'bytes' 解决方案

    用pycurl请求指定链接并返回结果时出现 TypeError: string argument expected, got 'bytes'  错误 经过排查问题出现在使用StringIO的write ...

  6. TypeError: write() argument must be str, not bytes报错

    TypeError: write() argument must be str, not bytes 之前文件打开的语句是: with open('C:/result.pk','w') as fp: ...

  7. TypeError: write() argument must be str, not bytes报错原因及Python3写入二进制文件方法

    Python2随机写入二进制文件: with open('/python2/random.bin','w') as f: f.write(os.urandom(10)) 但使用Python3会报错: ...

  8. python 在Unicode和普通字符串 str 之间转换

    unicodestring = u"Hello world" # 将Unicode转化为普通Python字符串:"encode" utf8string = un ...

  9. TypeError: write() argument must be str, not bytes

    w文件打开以 '二进制'  方式: with open('teacher.html','wb+') as f: f.write(response.body) 要写入"中文",防止乱 ...

随机推荐

  1. C# Global.asax文件里实现通用防SQL注入漏洞程序(适应于post/get请求)

    可使用Global.asax中的Application_BeginRequest(object sender, EventArgs e)事件来实现表单或者URL提交数据的获取,获取后传给SQLInje ...

  2. ThinkPHP如果表名有下划线需要用Model应该怎么做?

    最近遇到一个问题,在用TP做系统的时候,我建立的表是 “tp_admin_user” 但是要用到的模型是 “AdminUserModel.model.class.php”,应该如何做? 解决方法: & ...

  3. 记录用户操作历史命令history

    我们知道可以使用history命令,查看自己的操作记录,但如果你是root用户,如何查看其它用户的操作记录呢?   其实history命令只是把当前用户目录下的~/.bash_History文件内容列 ...

  4. MySQL数据的查询注意

    mysql数据库的查询结果输出的是一个记录,这一个记录是以字典的形式存储的,所以获取字段时要用字典提取数据的形式获取字段信息: 查询的代码: def query(self): # sql语句 sql ...

  5. tomcat的配置和优化

    tomcat的内存使用配置,最大连接数配置. 如何修改配置呢,在/tomcat的/bin/下面有个脚本文件catailna.sh. 如果 windows 是bat设置tomcat的使用内存,其实就是设 ...

  6. 前端调用后端的方法(基于restful接口的mvc架构)

    1.前端调用后台: 建议用你熟悉的一门服务端程序,例如ASP,PHP,JSP,C#这些都可以,然后把需要的数据从数据库中获得,回传给客户端浏览器(其实一般就是写到HTML中,或者生成XML文件)然后在 ...

  7. 第一课:初识Hadoop

    Hadoop核心组件之分布式文件系统HDFS: 特点:扩充性,容错性,海量数据存储. 在HDFS中每次上传文件,都会将文件切分成指定大小的数据块(默认128m)并以多副本的存储在多个机器上. 数据切分 ...

  8. 【开发者笔记】利用shp2pgsql将shape文件导入到postgresql中

    导入shp文件到postgresql中 1.首先,你需要让shp2pgsql命令可用,百度下载,加入环境变量即可. 下载地址:https://download.osgeo.org/postgis/wi ...

  9. MySql创建函数与过程,触发器, shell脚本与sql的相互调用。

    一:函数 1:创建数据库和表deptartment, mysql> use DBSC; Database changed mysql), ), )); Query OK, rows affect ...

  10. Linux服务器安装jdk+tomcat

    一.工具 1.1.SecureCRTSecureFX_7.0.0.326 下载地址:https://yunpan.cn/cRnxrv2eaQMwD  访问密码 a018 1.2.jdk 下载地址:问度 ...