解决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之正规地说一句话
Sep 28 Python
使用rpclib进行Python网络编程时的注释问题
May 06 Python
python实现简单爬虫功能的示例
Oct 24 Python
使用Python制作自动推送微信消息提醒的备忘录功能
Sep 06 Python
用python脚本24小时刷浏览器的访问量方法
Dec 07 Python
python基础梳理(一)(推荐)
Apr 06 Python
Python使用matplotlib实现交换式图形显示功能示例
Sep 06 Python
python os.path.isfile 的使用误区详解
Nov 29 Python
python GUI库图形界面开发之PyQt5拖放控件实例详解
Feb 25 Python
django 外键创建注意事项说明
May 20 Python
Python描述数据结构学习之哈夫曼树篇
Sep 07 Python
python制作图形界面的2048游戏, 基于tkinter
Apr 06 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中文乱码
2009/11/26 PHP
PHP mysql与mysqli事务使用说明 分享
2013/08/17 PHP
ThinkPHP框架设计及扩展详解
2014/11/25 PHP
php利用smtp类实现电子邮件发送
2015/10/30 PHP
jscript之Open an Excel Spreadsheet
2007/06/13 Javascript
JQuery 弹出框定位实现方法
2010/12/02 Javascript
简单漂亮的js弹窗可自由拖拽且兼容大部分浏览器
2013/10/22 Javascript
jquery submit ie6下失效的原因分析及解决方法
2013/11/15 Javascript
Jquery Ajax解析XML数据(同步及异步调用)简单实例
2014/02/12 Javascript
jquery取消选择select下拉框示例代码
2014/02/22 Javascript
jQuery在ul中显示某个li索引号的方法
2015/03/17 Javascript
javascript函数特点实例分析
2015/05/14 Javascript
再谈JavaScript异步编程
2016/01/27 Javascript
AngularJs bootstrap搭载前台框架——准备工作
2016/09/01 Javascript
jQuery中的insertBefore(),insertAfter(),after(),before()区别介绍
2016/09/01 Javascript
Babel 入门教程学习笔记
2018/06/13 Javascript
详解如何在vue-cli中使用vuex
2018/08/07 Javascript
详解Vue This$Store总结
2018/12/17 Javascript
详解Vue.js和layui日期控件冲突问题解决办法
2019/07/25 Javascript
JavaScript中如何调用Java方法
2020/09/16 Javascript
Python中的生成器和yield详细介绍
2015/01/09 Python
Python使用matplotlib实现绘制自定义图形功能示例
2018/01/18 Python
Python实现查找最小的k个数示例【两种解法】
2019/01/08 Python
python找出因数与质因数的方法
2019/07/25 Python
selenium+PhantomJS爬取豆瓣读书
2019/08/26 Python
详解在Python中以绝对路径或者相对路径导入文件的方法
2019/08/30 Python
手对手的教你用canvas画一个简单的海报的方法示例
2018/12/10 HTML / CSS
企业管理培训感言
2014/01/27 职场文书
十佳青年个人事迹材料
2014/01/28 职场文书
公司年终奖分配方案
2014/06/16 职场文书
党的群众路线批评与自我批评发言稿
2014/10/16 职场文书
党员民主生活会材料
2014/12/15 职场文书
2015年八一建军节慰问信
2015/03/23 职场文书
2015年医务科工作总结范文
2015/05/26 职场文书
PyTorch 如何检查模型梯度是否可导
2021/06/05 Python
Mysql如何实现不存在则插入,存在则更新
2022/03/25 MySQL