一个技术宅的个人博客

解决python问题 AttributeError “module ‘pymysql’ has no attribute ‘escape_string”

今天在用Python处理数据问题的时候,本地测试完全没有问题,到线上就提示module ‘pymysql’ has no attribute ‘escape_string,百度查了下,没有一个正确答案的,最后还是通过国外网站找到正确答案,记录下来,以后也许能用到,答案原文:https://www.tutorialexample.com/fix-python-attributeerror-module-pymysql-has-no-attribute-escape_string-python-tutorial/

翻译一下:

我们经常使用pymysql包来连接和操作python中的mysql数据库,但是,您也可能会遇到以下错误:AttributeError:module ‘pymysql’ has no attribute ‘escape_string。在本教程中,我们将介绍如何修复它。

先看看一下这段代码:

import pymysql

title = pymysql.escape_string(title)

运行此代码,可能会出现以下错误:

AttributeError:module ‘pymysql’ has no attribute ‘escape_string。

检查pymysql changelog,我们可以找到:

https://github.com/PyMySQL/PyMySQL/blob/master/CHANGELOG.md#v100

从pymysql 1.0.0模块中删除了转义\udc、escape\u序列和转义\u字符串

这意味着如果pymysql版本>=1.0.0,您将得到这个AttributeError。

检查pymysql版本

  1. print(pymysql.VERSION)

对我们来说,是1.0.2。我们得到这个错误。

如何修复此错误?

我们应该使用pymysql.converters。下面是一个示例代码。

  1. import pymysql
  2. from pymysql.converters import escape_string
  3. title = escape_string(title)

问题解决!

赞(15) 打赏
未经允许不得转载:孙健个人博客 » 解决python问题 AttributeError “module ‘pymysql’ has no attribute ‘escape_string”

评论 抢沙发

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

微信扫一扫打赏