解决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程序设计入门(3)数组的使用
Jun 16 Python
Python通过递归遍历出集合中所有元素的方法
Feb 25 Python
在SAE上部署Python的Django框架的一些问题汇总
May 30 Python
使用rst2pdf实现将sphinx生成PDF
Jun 07 Python
python求质数的3种方法
Sep 28 Python
python DataFrame 取差集实例
Jan 30 Python
三步实现Django Paginator分页的方法
Jun 11 Python
python能开发游戏吗
Jun 11 Python
django 实现后台从富文本提取纯文本
Jul 02 Python
Python常用类型转换实现代码实例
Jul 28 Python
深入理解python多线程编程
Apr 18 Python
python微信智能AI机器人实现多种支付方式
Apr 12 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
Window 7/XP 安装Apache 2.4与PHP 5.4 的过程详解
2013/06/02 PHP
PHP使用gmdate实现将一个UNIX 时间格式化成GMT文本的方法
2015/03/19 PHP
php中array_slice和array_splice函数解析
2016/10/18 PHP
PHP简单实现冒泡排序的方法
2016/12/26 PHP
php递归函数怎么用才有效
2018/02/24 PHP
如何优雅的使用 laravel 的 validator验证方法
2018/11/11 PHP
laravel使用Faker数据填充的实现方法
2019/04/12 PHP
用js+xml自动生成表格的东西
2006/12/21 Javascript
javascript JSON操作入门实例
2010/04/16 Javascript
jQuery之过滤元素操作小结
2013/11/30 Javascript
jquery实现动态画圆
2014/12/04 Javascript
学习Javascript面向对象编程之封装
2016/02/23 Javascript
javascript 分号总结及详细介绍
2016/09/24 Javascript
Angularjs中的ui-bootstrap的使用教程
2017/02/19 Javascript
微信小程序 判断手机号的实现代码
2017/04/19 Javascript
vue以组件或者插件的形式实现throttle或者debounce
2019/05/22 Javascript
在微信小程序中使用mqtt服务的方法
2019/12/13 Javascript
react中hook介绍以及使用教程
2020/12/11 Javascript
浅谈编码,解码,乱码的问题
2016/12/30 Python
Python基于递归算法求最小公倍数和最大公约数示例
2018/07/27 Python
Python cv2 图像自适应灰度直方图均衡化处理方法
2018/12/07 Python
Python变量访问权限控制详解
2019/06/29 Python
python openpyxl使用方法详解
2019/07/18 Python
python中利用numpy.array()实现俩个数值列表的对应相加方法
2019/08/26 Python
找Python安装目录,设置环境路径以及在命令行运行python脚本实例
2020/03/09 Python
Python numpy多维数组实现原理详解
2020/03/10 Python
python+requests接口自动化框架的实现
2020/08/31 Python
拉飞逸官网:Lafayette 148 New York
2020/07/15 全球购物
平面设计的岗位职责
2013/11/08 职场文书
党员个人公开承诺书
2014/08/29 职场文书
2014年作风建设心得体会
2014/10/22 职场文书
护林员个人总结
2015/03/04 职场文书
JMeter对MySQL数据库进行压力测试的实现步骤
2022/01/22 MySQL
面试中canvas绘制图片模糊图片问题处理
2022/03/13 Javascript
Mysql将字符串按照指定字符分割的正确方法
2022/05/30 MySQL
Oracle锁表解决方法的详细记录
2022/06/05 Oracle