解决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 09 Python
教大家使用Python SqlAlchemy
Feb 12 Python
Python全排列操作实例分析
Jul 24 Python
Python使用Shelve保存对象方法总结
Jan 28 Python
Python操作Mongodb数据库的方法小结
Sep 10 Python
Python count函数使用方法实例解析
Mar 23 Python
python变量的作用域是什么
May 26 Python
用Python爬取LOL所有的英雄信息以及英雄皮肤的示例代码
Jul 13 Python
python 自动刷新网页的两种方法
Apr 20 Python
pandas:get_dummies()与pd.factorize()的用法及区别说明
May 21 Python
python基础入门之字典和集合
Jun 13 Python
Python干货实战之八音符酱小游戏全过程详解
Oct 24 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/09/28 PHP
php性能优化分析工具XDebug 大型网站调试工具
2011/05/22 PHP
PHP大小写问题:函数名和类名不区分,变量名区分
2013/06/17 PHP
PHP+jquery+CSS制作头像登录窗(仿QQ登陆)
2016/10/20 PHP
PHP注释语法规范与命名规范详解篇
2018/01/21 PHP
laravel 实现划分admin和home 模块分组
2019/10/15 PHP
ExtJS4 组件化编程,动态加载,面向对象,Direct
2011/05/12 Javascript
jQuery Jcrop插件实现图片选取功能
2011/11/23 Javascript
fancybox modal的完美解决(右上的X)
2012/10/30 Javascript
控制页面按钮在后台执行期间不重复提交的JS方法
2013/06/24 Javascript
使用GruntJS链接与压缩多个JavaScript文件过程详解
2013/08/02 Javascript
JS实现时间格式化的方式汇总
2013/10/16 Javascript
JS实现拖动示例代码
2013/11/01 Javascript
javascript与jquery中跳出循环的区别总结
2013/11/04 Javascript
jquery设置按钮停顿3秒不可用
2014/03/07 Javascript
javascript的事件触发器介绍的实现
2014/06/05 Javascript
js构造函数、索引数组和属性的实现方式和使用
2014/11/16 Javascript
jQuery中DOM树操作之使用反向插入方法实例分析
2015/01/23 Javascript
JavaScript中length属性的使用方法
2015/06/05 Javascript
JS仿hao123导航页面图片轮播效果
2016/09/01 Javascript
利用jquery给指定的table动态添加一行、删除一行的方法
2016/10/12 Javascript
Vue父组件调用子组件事件方法
2018/02/23 Javascript
在vue+element ui框架里实现lodash的debounce防抖
2019/11/13 Javascript
swiperjs实现导航与tab页的联动
2020/12/13 Javascript
python一行sql太长折成多行并且有多个参数的方法
2018/07/19 Python
python生成带有表格的图片实例
2019/02/03 Python
Selenium结合BeautifulSoup4编写简单的python爬虫
2020/11/06 Python
一套Java笔试题
2016/08/20 面试题
物理专业大学生职业生涯规划书
2014/02/07 职场文书
信息与计算科学专业推荐信
2014/02/23 职场文书
国贸专业自荐信范文
2014/03/02 职场文书
前台文员职责范本
2014/03/07 职场文书
幼儿园新生开学寄语
2015/05/27 职场文书
先进个人事迹材料(2016推荐版)
2016/03/01 职场文书
民事调解协议书
2016/03/21 职场文书
适合毕业生创业的项目怎么找?
2019/08/08 职场文书