解决python3插入mysql时内容带有引号的问题


Posted in Python onMarch 02, 2020

插入mysql时,如果内容中有引号等特殊符号,会报错,

解决方法可以用反斜杠转义,还可以用pymysql的一个方法自动转义:

c = '''

北京时间9月20日晚间9点半,智能供应链服务供应商百世集团将在<a class="wt_article_link" onmouseover="WeiboCard.show(2125973432,'tech',this)" href="?zw=tech" rel="external nofollow" target="_blank">纽约证券交易所</a>正式挂牌上市,交易代码为“BSTI”。这是继<span id="usstock_ZTO"><a href="http://stock.finance.sina.com.cn/usstock/quotes/ZTO.html" rel="external nofollow" class="keyword f_st" target="_blank">中通</a></span><span id=quote_ZTO></span>快递之后第二家赴美上市的快递物流企业。 </p>
<p>

此次IPO百世集团一共发行4500万股美国存托股份(ADS),每股价格为10美元,总融资额高达4.5亿美元,为今年目前为止在美国上市的中国公司中募资规模最大的IPO。此外,百世和售股股东还允许其承销商通过超额配售权购买额外不多于675万股ADS。</p>
<p>

有中通这个“珠玉”在前,美股市场似'''

pymysql.escape_string(c)

sql = "INSERT INTO tbl_stream_copy(weburl,title,content,channelId,datetime,pubtime,website)VALUES ('%s','%s',\'%s\','%s','%s','%s','%s')" % (a,b,pymysql.escape_string(c),e,datetime,datetime,a)

补充拓展:Python中执行MySQL语句, 遇到同时有单引号, 双引号处理方式 !r, repr()

SQL语句:

insert_cmd = "INSERT INTO {0} SET {1}"
.format(db_conn.firmware_info_table, 
  ','.join(['{0}={1!r}'.format(k, str(v)) for (k, v) in info_dict.items()]))

其中{0}={1!r} 作用是设置字段的值,一般情况应该是:

{0}='{1}'.format(columnA, value)

但若value中同时有双引号和单引号("", ''),比如{'abc': '123', "def": "456"},

则会在execute(insert_cmd)时报错。

如果想保持数据原始性,不使用replace替换成统一的单引号或者双引号,

则可以使用!r来调用repr() 函数, 将对象转化为供解释器读取的形式。

repr() 返回一个对象的 string 格式。

!r 表示使用repr()替代默认的str()来返回。

注:repr是str的方法,所以value需要是string,若数据是dict等类型,需要使用str()转换成string

According to the Python 2.7.12 documentation:
!s (apply str()) and !r (apply repr()) can be used to convert the value before it is formatted.

贴出str类中的repr说明:

repr(object)
Return a string containing a printable representation of an object.
This is the same value yielded by conversions(reverse quotes).
It is sometimes useful to be able to access this operation as an ordinary function.
For many types, this function makes an attempt to return a string that would yield
an object with the same value when passed to eval(),
otherwise the representation is a string enclosed in angle brackets
that contains the name of the type of the object together with additional information
often including the name and address of the object. A class can control what this function
returns for its instances by defining a __repr__() method.

以上这篇解决python3插入mysql时内容带有引号的问题就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持三水点靠木。

Python 相关文章推荐
python获取文件扩展名的方法
Jul 06 Python
Python编程中对super函数的正确理解和用法解析
Jul 02 Python
Python set常用操作函数集锦
Nov 15 Python
Python中的默认参数实例分析
Jan 29 Python
Python request设置HTTPS代理代码解析
Feb 12 Python
Django处理多用户类型的方法介绍
May 18 Python
Python将文字转成语音并读出来的实例详解
Jul 15 Python
python中for循环把字符串或者字典添加到列表的方法
Jul 20 Python
Django 开发调试工具 Django-debug-toolbar使用详解
Jul 23 Python
python飞机大战pygame游戏之敌机出场实现方法详解
Dec 17 Python
Python中的wordcloud库安装问题及解决方法
May 27 Python
Python 实现定积分与二重定积分的操作
May 26 Python
python统计字符串中字母出现次数代码实例
Mar 02 #Python
python绘制玫瑰的实现代码
Mar 02 #Python
pymysql 插入数据 转义处理方式
Mar 02 #Python
python实现字符串和数字拼接
Mar 02 #Python
Python通过正则库爬取淘宝商品信息代码实例
Mar 02 #Python
基于Python爬取爱奇艺资源过程解析
Mar 02 #Python
python GUI库图形界面开发之PyQt5树形结构控件QTreeWidget详细使用方法与实例
Mar 02 #Python
You might like
用PHP实现ODBC数据分页显示一例
2006/10/09 PHP
php self,$this,const,static,-&amp;gt;的使用
2009/10/22 PHP
PHP抽象类 介绍
2012/06/13 PHP
解析php通过cookies获取远程网页的指定代码
2013/06/25 PHP
php用正则表达式匹配中文实例详解
2013/11/06 PHP
PHP基于GD库的缩略图生成代码(支持jpg,gif,png格式)
2014/06/19 PHP
PHP实现根据银行卡号判断银行
2015/04/29 PHP
php监测数据是否成功插入到Mysql数据库的方法
2016/11/25 PHP
利用php-cli和任务计划实现订单同步功能的方法
2017/05/03 PHP
利用PHPExcel读取Excel的数据和导出数据到Excel
2017/05/12 PHP
Discuz不使用插件实现简单的打赏功能
2019/03/21 PHP
简单的JS多重继承示例
2008/03/13 Javascript
jQuery动态地获取系统时间实现代码
2013/05/24 Javascript
js open() 与showModalDialog()方法使用介绍
2013/09/10 Javascript
JS获取各种宽度、高度的简单介绍
2014/12/19 Javascript
Javascript闭包用法实例分析
2015/01/23 Javascript
JavaScript驾驭网页-CSS与DOM
2016/03/24 Javascript
浅谈JS验证表单文本域输入空格的问题
2017/02/14 Javascript
js实现时间轴自动排列效果
2017/03/09 Javascript
谈谈为什么你的 JavaScript 代码如此冗长
2019/01/30 Javascript
vue中v-text / v-html使用实例代码详解
2019/04/02 Javascript
[44:50]2018DOTA2亚洲邀请赛 4.1 小组赛 A组 TNC vs VG
2018/04/02 DOTA
[02:12]2019完美世界全国高校联赛(春季赛)报名开启
2019/03/01 DOTA
Python爬取Coursera课程资源的详细过程
2014/11/04 Python
Python lxml模块安装教程
2015/06/02 Python
python自定义异常实例详解
2017/07/11 Python
python实现飞机大战
2018/09/11 Python
党课培训主持词
2014/04/01 职场文书
地球一小时倡议书
2014/04/15 职场文书
大学学风建设方案
2014/05/04 职场文书
房屋租赁合同协议书范本
2014/10/19 职场文书
2014年酒店工作总结范文
2014/11/17 职场文书
2015年部门工作总结范文
2015/03/31 职场文书
thinkphp 获取控制器及控制器方法
2021/04/16 PHP
浅谈Python3中datetime不同时区转换介绍与踩坑
2021/08/02 Python
分享一个vue实现的记事本功能案例
2022/04/11 Vue.js