解决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中使用ConfigParser解析ini配置文件实例
Aug 30 Python
详解Python中expandtabs()方法的使用
May 18 Python
Python合并两个字典的常用方法与效率比较
Jun 17 Python
使用Python来开发Markdown脚本扩展的实例分享
Mar 04 Python
Python中list初始化方法示例
Sep 18 Python
Python探索之爬取电商售卖信息代码示例
Oct 27 Python
Django分页查询并返回jsons数据(中文乱码解决方法)
Aug 02 Python
Linux 修改Python命令的方法示例
Dec 03 Python
python pandas库的安装和创建
Jan 10 Python
python-xpath获取html文档的部分内容
Mar 06 Python
Python爬虫headers处理及网络超时问题解决方案
Jun 19 Python
详解Python为什么不用设计模式
Jun 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 伪造ip以及url来路信息方法汇总
2014/11/25 PHP
PHP封装的字符串加密解密函数
2015/12/18 PHP
PHP中soap用法示例【SoapServer服务端与SoapClient客户端编写】
2018/12/25 PHP
Avengerls vs Newbee BO3 第一场2.18
2021/03/10 DOTA
模仿JQuery sortable效果 代码有错但值得看看
2009/11/05 Javascript
javasctipt如何显示几分钟前、几天前等
2014/04/30 Javascript
node.js中的console.dir方法使用说明
2014/12/10 Javascript
js+jquery实现图片裁剪功能
2015/01/02 Javascript
基于jQuery实现拖拽图标到回收站并删除功能
2015/11/25 Javascript
Javascript实现鼠标框选操作  不是点击选取
2016/04/14 Javascript
在Html中使用Requirejs进行模块化开发实例详解
2016/04/15 Javascript
javascript基础知识
2016/06/07 Javascript
JS关闭窗口时产生的事件及用法示例
2016/08/20 Javascript
自己封装的一个简单的倒计时功能实例
2016/11/23 Javascript
vue中使用refs定位dom出现undefined的解决方法
2017/12/21 Javascript
使用vue.js在页面内组件监听scroll事件的方法
2018/09/11 Javascript
用Vue.js方法创建模板并使用多个模板合成
2019/06/28 Javascript
基于vue-cli3创建libs库的实现方法
2019/12/04 Javascript
在Vue里如何把网页的数据导出到Excel的方法
2020/09/30 Javascript
Python functools模块学习总结
2015/05/09 Python
wxPython实现窗口用图片做背景
2018/04/25 Python
python设置值及NaN值处理方法
2018/07/03 Python
python pandas实现excel转为html格式的方法
2018/10/23 Python
Django 导出项目依赖库到 requirements.txt过程解析
2019/08/23 Python
Python+pyftpdlib实现局域网文件互传
2020/08/24 Python
简单了解Python字典copy与赋值的区别
2020/09/16 Python
Python reversed反转序列并生成可迭代对象
2020/10/22 Python
celery在python爬虫中定时操作实例讲解
2020/11/27 Python
canvas 实现 github404动态效果的示例代码
2017/11/15 HTML / CSS
美国卡车、吉普车和SUV零件网站:4 Wheel Parts
2016/11/24 全球购物
香港连卡佛百货官网:Lane Crawford
2019/09/04 全球购物
秘书专业自荐信范文
2013/12/26 职场文书
大四学生思想汇报
2014/01/13 职场文书
小学语文国培研修日志
2015/11/13 职场文书
你真的会用Mysql的explain吗
2022/03/31 MySQL
Win11怎么跳过联网验机 ?Win11跳过联网验机激活教程
2022/04/05 数码科技