IT俱乐部 Python 解决Python TypeError: an integer is required (got type str)问题及过程

解决Python TypeError: an integer is required (got type str)问题及过程

今天调试代码遇到的问题

TypeError: an integer is required (got type str)

问题原因

在python3的打开文件对象的open函数,

以下为错误写法:

 read_helper=open(checkpath,"r","utf-8")

对于第三个参数,指定编码方式必须要加encoding=“utf-8”

read_helper=open(checkpath,"r",encoding="utf-8")

如果不加encoding=,则解释器默认认为第三个参数为指定buffering,

这个变量本身要传入一个整形变量才行:

def open(file, mode='r', buffering=None, encoding=None, errors=None, newline=None, closefd=True):

而对于二进制读取的模式,则不能制定编码方法,

正确写法如下:

read_helper=open(checkpath,"rb")

总结

以上为个人经验,希望能给大家一个参考,也希望大家多多支持IT俱乐部。

本文收集自网络,不代表IT俱乐部立场,转载请注明出处。https://www.2it.club/code/python/18658.html
上一篇
下一篇
联系我们

联系我们

在线咨询: QQ交谈

邮箱: 1120393934@qq.com

工作时间:周一至周五,9:00-17:30,节假日休息

关注微信
微信扫一扫关注我们

微信扫一扫关注我们

返回顶部