Python Bug: TypeError: a bytes-like object is required, not ‘str

  • 背景
  • 解决方法:

背景

data_np = np.loadtxt(os.path.join(data_path, data_name), delimiter=',', skiprows=1, converters={1: lambda x: float(x.split('/')[-1])}, dtype=np.float32)

如上所示,该行代码用于读取表格数据, 其中converters参数用于将表中第1列的日期类型数据xxxx/xx/xx截取最后日期并转为数值类型,运行出现上述错误。

解决方法:

原因: 主要设计byte和str数据类型的转换,在调用split之前首先要通过.decode()方法对字符串以指定的编码格式(默认encoding=‘utf-8’)解码。encode()作用正好相反。

data_np = np.loadtxt(os.path.join(data_path, data_name), delimiter=',', skiprows=1, converters={1: lambda x: float(x.decode().split('/')[-1])}, dtype=np.float32)

Python Bug: TypeError: a bytes-like object is required, not ‘str相关推荐

  1. python write报错a byte-like object is required.not str

    办法一:根本原因是Python版本问题python2.x中要求用'wb',python3.x中要求用'w' 用open(filename,'w')方式 办法二:encode('ascii')

  2. Python Socket TypeError: a bytes-like object is required, not 'str' 错误提示

    <python核心编程>第三版,发现示例2-1代码返回错误-..纠结很久  发现这里python3.5和Python2.7在套接字返回值解码上有区别.  先介绍一下 python byte ...

  3. Python 3.5.2 TypeError: a bytes-like object is required, not 'str’问题解决方案

    运行环境Mac  Python 3.5.2 Q: http_response = """\ HTTP/1.1 200 OK Hello, World! "&qu ...

  4. Python异常:TypeError: a bytes-like object is required, not 'str'

    Python异常:TypeError: a bytes-like object is required, not 'str' import jsonstr = [{"userName&quo ...

  5. 成功解决TypeError: a bytes-like object is required, not 'str'

    成功解决TypeError: a bytes-like object is required, not 'str' 目录 解决问题 解决思路 解决方法 解决问题 TypeError: a bytes- ...

  6. 生成测试报告时遇到问题TypeError: a bytes-like object is required, not ‘str‘

    项目场景: 场景:使用python做自动化测试,用HTMLTestRunner生成自动化测试报告 问题描述 例如:做自动化测试时,用HTMLTestRunner生成自动化测试报告,报错TypeErro ...

  7. 成功解决TypeError: a bytes-like object is required, not ‘str‘

    出现问题: 在文件写操作时出现TypeError: a bytes-like object is required, not 'str' 意思是函数write需要的参数类型是字节对象不是字符串 解决办 ...

  8. Python更新数据库,解决转译字符与TypeError: XX format: a number is required, not str报错

    在使用python更新数据库的时候出现了好多坑,一个个填完之后整理出了完备的代码: import MySQLdb#链接数据库的套路操作 db = MySQLdb.connect("local ...

  9. a bytes-like object is required, not 'str'

    TypeError: a bytes-like object is required, not 'str' 问题分析 该问题主要是由于当前操作的字符串是bytes类型的字符串对象,并对该bytes类型 ...

  10. a bytes-like object is required, not 'str' pickle.loads pickle.dumps

    a bytes-like object is required, not 'str'  pickle.loads pickle.dumps pickle可以序列化任何简单对象. 解决方法:

最新文章

  1. 如果一个按钮被覆盖如何响应?
  2. 使用Java基于数据流直接抽取word文本
  3. centos sudo不能运行_如何在 Linux 中配置 sudo 访问权限 | Linux 中国
  4. 电商教父李国庆的另类儒商气质:耿直与真诚
  5. 鄙人之斗,读IT之洋(1)
  6. Python 之打包工具 setup.py
  7. 【树莓派】【网摘】树莓派与XBMC及Kodi、LibreELEC插件(三)
  8. 荣耀5G手机加速追赶:最快将于下半年登场
  9. Android About ContentProvider
  10. 【慢慢学算法】:八进制(vector练习)
  11. 四川大学软件学院操作系统笔记
  12. 劳务派遣计算机信息管理系统,劳务派遣人员信息管理系统
  13. uni-app 实现自定义音效播放
  14. 如何用burpsuite和手机模拟器给apk抓包
  15. nginx配置服务器
  16. kali清理_linux下清理系统垃圾
  17. 【JavaScript】JS高级-面向对象编程
  18. [ICCV2021] TransReID: Transformer-based Object Re-Identification 行人重识别
  19. outlook客户端怎么看html,谷歌浏览器根据html网页启动邮件客户端Outlook(示例代码)...
  20. Vue进阶(幺贰叁):v-for 实现一行展示 n 个元素

热门文章

  1. [Swift]LeetCode145. 二叉树的后序遍历 | Binary Tree Postorder Traversal
  2. JavaScript学习心得04
  3. 初学Laravel框架与ThinkPHP框架的不同
  4. HDU4565(SummerTrainingDay05-C 矩阵快速幂)
  5. 问题-[致命错误] Project1.dpr(1): Unit not found: 'System.pas' or binary equivalents (DCU,DPU)
  6. 那年,寻找工作的历程
  7. 在uniapp或者vue中,单行文字或者数字无法换行导致后面内容无法展示问题的解决方案
  8. 微博返回顶部按钮实现方法大剧透
  9. IOS14微信小程序scroll-view 不滚动
  10. 超详细汇总21个mysql优化实践【收藏版MySQL优化】