当前位置: 首页 > news >正文

Python上解决TypeError: not all arguments converted during string formatting错误

目录

  • 背景
  • 尝试1: pymysql模块的escape_string方法
  • 尝试2: 修改pandas.read_excel引擎
  • 尝试3: 回退xlrd版本
  • 总结


背景

在Linux上部署的时候, 使用pandas模块读取Excel, 然后pymysql模块入库, 结果发生了错误

 Traceback (most recent call last):File "/usr/local/lib64/python3.6/site-packages/tornado/web.py", line 1702, in _executeresult = method(*self.path_args, **self.path_kwargs)File "/root/.jenkins/workspace/git_python_web_linux/handler.py", line 1135, in postself._replyPrivateMessage(message)File "/root/.jenkins/workspace/git_python_web_linux/handler.py", line 1221, in _replyPrivateMessageself._junming(filepath)File "/root/.jenkins/workspace/git_python_web_linux/handler.py", line 1263, in _junmingsdk.mysql(config.db_qihua).insmany('qihua.log_bjmantis_cost',data_db)File "/root/.jenkins/workspace/git_python_web_linux/sdk.py", line 65, in insmanyself._cur.executemany(sqlInsert,listTuple)File "/usr/local/lib64/python3.6/site-packages/pymysql/cursors.py", line 182, in executemanyself.rowcount = sum(self.execute(query, arg) for arg in args)File "/usr/local/lib64/python3.6/site-packages/pymysql/cursors.py", line 182, in <genexpr>self.rowcount = sum(self.execute(query, arg) for arg in args)File "/usr/local/lib64/python3.6/site-packages/pymysql/cursors.py", line 146, in executequery = self.mogrify(query, args)File "/usr/local/lib64/python3.6/site-packages/pymysql/cursors.py", line 125, in mogrifyquery = query % self._escape_args(args, conn)TypeError: not all arguments converted during string formatting

尝试1: pymysql模块的escape_string方法

from pymysql.converters import escape_string
...
data_database = [escape_string(x) for x in data]
...

还是报错, 所以应该不是字符的问题.
打印出来肉眼观察, 也没有字符问题


尝试2: 修改pandas.read_excel引擎

修改引擎, 是可以解决字符问题, 但出现了新的问题

...
excel = pandas.read_excel(filepath, engine='openpyxl')
...

正常入库. 但是, 无法读取xls文件

Traceback (most recent call last):File "/usr/local/lib64/python3.6/site-packages/tornado/web.py", line 1702, in _executeresult = method(*self.path_args, **self.path_kwargs)File "/root/.jenkins/workspace/git_python_web_linux/handler.py", line 1135, in postself._replyPrivateMessage(message)File "/root/.jenkins/workspace/git_python_web_linux/handler.py", line 1224, in _replyPrivateMessageself._qingzhilan(filepath)File "/root/.jenkins/workspace/git_python_web_linux/handler.py", line 1296, in _qingzhilandf = pandas.read_excel(filepath,sheet_name=['Sheet1'], engine='openpyxl')File "/usr/local/lib64/python3.6/site-packages/pandas/util/_decorators.py", line 296, in wrapperreturn func(*args, **kwargs)File "/usr/local/lib64/python3.6/site-packages/pandas/io/excel/_base.py", line 304, in read_excelio = ExcelFile(io, engine=engine)File "/usr/local/lib64/python3.6/site-packages/pandas/io/excel/_base.py", line 867, in __init__self._reader = self._engines[engine](self._io)File "/usr/local/lib64/python3.6/site-packages/pandas/io/excel/_openpyxl.py", line 480, in __init__super().__init__(filepath_or_buffer)File "/usr/local/lib64/python3.6/site-packages/pandas/io/excel/_base.py", line 353, in __init__self.book = self.load_workbook(filepath_or_buffer)File "/usr/local/lib64/python3.6/site-packages/pandas/io/excel/_openpyxl.py", line 492, in load_workbookfilepath_or_buffer, read_only=True, data_only=True, keep_links=FalseFile "/usr/local/lib/python3.6/site-packages/openpyxl/reader/excel.py", line 345, in load_workbookdata_only, keep_links, rich_text)File "/usr/local/lib/python3.6/site-packages/openpyxl/reader/excel.py", line 123, in __init__self.archive = _validate_archive(fn)File "/usr/local/lib/python3.6/site-packages/openpyxl/reader/excel.py", line 93, in _validate_archiveraise InvalidFileException(msg)openpyxl.utils.exceptions.InvalidFileException: openpyxl does not support the old .xls file format, please use xlrd to read this file, or convert it to the more recent .xlsx file format.

尝试3: 回退xlrd版本

注: Linux平台上默认应该用pip3替代pip

pip list | grep xlrd # 查看现有xlrd版本
# pip list | findstr 'xlrd' ## Windows平台查包
pip uninstall xlrd # 卸载包
pip install xlrd==1.2.0 # 安装包

回退到1.2.0版本后,我之前遇到的错误消失了。我的代码再次可以正常运行,我能够继续我的数据分析工作。


总结

Linux平台使用pandas.read_excel并且可能会有xlsxlsx格式的话, 建议回退xlrd版本到1.2.0来解决字符和格式问题


请添加图片描述

http://www.lryc.cn/news/336784.html

相关文章:

  • ASUS华硕ROG幻16Air笔记本电脑GU605M原装出厂Win11系统工厂包下载,带有ASUSRecovery一键重置还原
  • 【OpenVINO™】使用 OpenVINO™ C# API 部署 YOLOv9 目标检测和实例分割模型(上篇)
  • 代码随想录——二分查找(一)
  • 【NLP】多标签分类【下】
  • HWOD:密码强度等级
  • 期货学习笔记-MACD指标学习2
  • 5G智慧港口简介(一)
  • 订单中台架构:打造高效订单管理系统的关键
  • 【算法】模拟
  • 电力综合自动化系统对电力储能技术的影响有哪些?
  • Compose UI 之 Card 卡片组件
  • ELK日志
  • WPF Pack
  • 计算两个时间段的差值
  • Element Plus 表单校验
  • java实现TCP交互
  • 学习云计算HCIE选择誉天有什么优势?
  • python之文件操作与管理
  • 大厂Java笔试题之对完全数的处理
  • 【Redis深度解析】揭秘Cluster(集群):原理、机制与实战优化
  • 【JAVA基础篇教学】第六篇:Java异常处理
  • 【ubuntu20.04】安装GeographicLib
  • 从0开始搭建基于VUE的前端项目(四) Vue-Router的使用与配置
  • 力扣爆刷第117天之CodeTop100五连刷71-75
  • ActiveMQ入门案例(queue模式和topic模式)
  • 2024年最新云服务器ECS租用报价费用表-阿里云
  • 第四百五十四回
  • 蓝桥杯算法题:蓝桥骑士
  • sonar搭建(linux系统)
  • 中科软面试题