解决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 相关文章推荐
在Windows服务器下用Apache和mod_wsgi配置Python应用的教程
May 06 Python
机器学习python实战之决策树
Nov 01 Python
python爬虫爬取网页表格数据
Mar 07 Python
Python json模块dumps、loads操作示例
Sep 06 Python
Python中函数的基本定义与调用及内置函数详解
May 13 Python
将Python字符串生成PDF的实例代码详解
May 17 Python
Python 生成器,迭代,yield关键字,send()传参给yield语句操作示例
Oct 12 Python
python groupby 函数 as_index详解
Dec 16 Python
python脚本实现mp4中的音频提取并保存在原目录
Feb 27 Python
在python中利用pycharm自定义代码块教程(三步搞定)
Apr 15 Python
Python如何输出警告信息
Jul 30 Python
python中PyQuery库用法分享
Jan 15 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 Undefined index和Undefined variable的解决方法
2008/03/27 PHP
PHP封装函数实现生成随机的字符串验证码
2017/01/24 PHP
JS模拟多线程
2007/02/07 Javascript
jQuery.Autocomplete实现自动完成功能(详解)
2010/07/13 Javascript
javascript AOP 实现ajax回调函数使用比较方便
2010/11/20 Javascript
基于JQuery的日期联动实现代码
2011/02/24 Javascript
解析js如何获取当前url中的参数值并复制给input
2013/06/23 Javascript
JS获取计算机mac地址以及IP的实现方法
2014/01/08 Javascript
jQuery控制TR显示隐藏的几种方法
2014/06/18 Javascript
Javascript 读取操作Sql中的Xml字段
2014/10/09 Javascript
jQuery子属性过滤选择器用法分析
2015/02/10 Javascript
使用javascript实现判断当前浏览器
2015/04/14 Javascript
javascript处理a标签超链接默认事件的方法
2015/06/29 Javascript
javascript中JSON对象与JSON字符串相互转换实例
2015/07/11 Javascript
全面理解JavaScript中的闭包
2016/05/12 Javascript
jQuery无刷新上传之uploadify3.1简单使用
2016/06/18 Javascript
$.browser.msie 为空或不是对象问题的多种解决方法
2017/03/19 Javascript
JavaScript转换数据库DateTime字段类型方法
2017/06/27 Javascript
node.js+captchapng+jsonwebtoken实现登录验证示例
2017/08/17 Javascript
日期时间范围选择插件:daterangepicker使用总结(必看篇)
2017/09/14 Javascript
Nodejs 数组的队列以及forEach的应用详解
2021/02/25 NodeJs
Python根据文件名批量转移图片的方法
2018/10/21 Python
利用pytorch实现对CIFAR-10数据集的分类
2020/01/14 Python
python爬虫实现POST request payload形式的请求
2020/04/30 Python
linux系统下pip升级报错的解决方法
2021/01/31 Python
浅析HTML5:'data-'属性的作用
2018/01/23 HTML / CSS
英国领先的露营和露营车品牌之一:OLPRO
2019/08/06 全球购物
材料化学应届生求职信
2013/10/09 职场文书
浙江文明网签名寄语
2014/01/18 职场文书
房地产开盘策划方案
2014/02/10 职场文书
挖掘机司机岗位职责
2014/02/12 职场文书
给校长的建议书500字
2014/05/15 职场文书
上海世博会口号
2014/06/19 职场文书
护士工作失误检讨书
2014/09/14 职场文书
如何起草一份正确的合伙创业协议书?
2019/07/04 职场文书
详解nginx进程锁的实现
2021/06/14 Servers