解决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写日志封装类实例
Jun 28 Python
Python虚拟环境virtualenv的安装与使用详解
May 28 Python
Python实现螺旋矩阵的填充算法示例
Dec 28 Python
Python使用Dijkstra算法实现求解图中最短路径距离问题详解
May 16 Python
数据清洗--DataFrame中的空值处理方法
Jul 03 Python
python3.4控制用户输入与输出的方法
Oct 17 Python
python爬取淘宝商品销量信息
Nov 16 Python
Tensorflow训练模型越来越慢的2种解决方案
Feb 07 Python
Python3 元组tuple入门基础
Feb 09 Python
Python通过类的组合模拟街道红绿灯
Sep 16 Python
matplotlib部件之矩形选区(RectangleSelector)的实现
Feb 01 Python
高考要来啦!用Python爬取历年高考数据并分析
Jun 03 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
Terran热键控制
2020/03/14 星际争霸
php file_get_contents函数轻松采集html数据
2010/04/22 PHP
Windows下XDebug 手工配置与使用说明
2010/07/11 PHP
PHP 微信扫码支付源代码(推荐)
2016/11/03 PHP
laravel 使用auth编写登录的方法
2019/09/30 PHP
js 输出内容到新窗口具体实现代码
2013/05/31 Javascript
动态读取JSON解析键值对的方法
2014/06/03 Javascript
js实现跟随鼠标移动且带关闭功能的图片广告实例
2015/02/26 Javascript
动态加载JavaScript文件的两种方法
2016/04/22 Javascript
js智能获取浏览器版本UA信息的方法
2016/08/08 Javascript
backbone简介_动力节点Java学院整理
2017/07/14 Javascript
JavaScript实现旋转轮播图
2020/08/18 Javascript
基于angular6.0实现的一个组件懒加载功能示例
2018/04/12 Javascript
JS操作字符串转数字的常见方法示例
2019/10/29 Javascript
浅析vue-router实现原理及两种模式
2020/02/11 Javascript
vue 解决兄弟组件、跨组件深层次的通信操作
2020/07/27 Javascript
仿照Element-ui实现一个简易的$message方法
2020/09/14 Javascript
Python的“二维”字典 (two-dimension dictionary)定义与实现方法
2016/04/27 Python
Python minidom模块用法示例【DOM写入和解析XML】
2019/03/25 Python
python获取指定日期范围内的每一天,每个月,每季度的方法
2019/08/08 Python
PyTorch中的padding(边缘填充)操作方式
2020/01/03 Python
Python基础教程之输入输出和运算符
2020/07/26 Python
Python监听剪切板实现方法代码实例
2020/11/11 Python
python实现PolynomialFeatures多项式的方法
2021/01/06 Python
比利时网上药店: Drogisterij.net
2017/03/17 全球购物
送给他或她的礼物:FUN.com
2018/08/17 全球购物
瑞典网上购买现代和复古家具:Reforma
2019/10/21 全球购物
英国最大的天然和有机产品在线零售商之一:Big Green Smile
2020/05/06 全球购物
构建高效课堂实施方案
2014/03/13 职场文书
《大自然的语言》教学反思
2014/04/08 职场文书
离退休人员聘用协议书
2014/11/24 职场文书
交通处罚决定书
2015/06/24 职场文书
2015暑期社会实践通讯稿
2015/07/18 职场文书
分析ZooKeeper分布式锁的实现
2021/06/30 Java/Android
Python中 range | np.arange | np.linspace三者的区别
2022/03/22 Python
python神经网络 使用Keras构建RNN训练
2022/05/04 Python