解决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实现zencart产品数据导入到magento(python导入数据)
Apr 03 Python
python logging 日志轮转文件不删除问题的解决方法
Aug 02 Python
详解python里使用正则表达式的分组命名方式
Oct 24 Python
详解Python里使用正则表达式的ASCII模式
Nov 02 Python
Python 的AES加密与解密实现
Jul 09 Python
解决python3 安装不了PIL的问题
Aug 16 Python
python并发编程 Process对象的其他属性方法join方法详解
Aug 20 Python
基于python+selenium的二次封装的实现
Jan 06 Python
Python使用type动态创建类操作示例
Feb 29 Python
python pymysql链接数据库查询结果转为Dataframe实例
Jun 05 Python
Python Tricks 使用 pywinrm 远程控制 Windows 主机的方法
Jul 21 Python
Python数据可视化之绘制柱状图和条形图
May 25 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 json_decode函数详细解析
2014/02/17 PHP
简单介绍win7下搭建apache+php+mysql开发环境
2015/08/06 PHP
PHP命名空间和自动加载类
2016/04/03 PHP
一个非常实用的php文件上传类
2017/07/04 PHP
PHP上传图片到数据库并显示的实例代码
2019/12/20 PHP
checkbox 复选框不能为空
2009/07/11 Javascript
JavaScript面向对象之静态与非静态类
2010/02/03 Javascript
滚动图片效果 jquery实现回旋滚动效果
2013/01/08 Javascript
jquery实现select选中行、列合计示例
2014/04/25 Javascript
jQuery验证插件 Validate详解
2014/11/20 Javascript
JS实现选项卡实例详解
2015/11/17 Javascript
详解webpack编译多页面vue项目的配置问题
2017/12/11 Javascript
js构建二叉树进行数值数组的去重与优化详解
2018/03/26 Javascript
基于Vue实现拖拽功能
2020/07/29 Javascript
javascript、php关键字搜索函数的使用方法
2018/05/29 Javascript
layui弹出层按钮提交iframe表单的方法
2018/08/20 Javascript
vue实现文字横向无缝走马灯组件效果的实例代码
2019/04/09 Javascript
Webpack4+Babel7+ES6兼容IE8的实现
2019/04/10 Javascript
jQuery层叠选择器用法实例分析
2019/06/28 jQuery
微信小程序用户盒子、宫格列表的实现
2020/07/01 Javascript
vue实现把接口单独存放在一个文件方式
2020/08/13 Javascript
vue在App.vue文件中监听路由变化刷新页面操作
2020/08/14 Javascript
Python和GO语言实现的消息摘要算法示例
2015/03/10 Python
100行Python代码实现每天不同时间段定时给女友发消息
2019/09/27 Python
详解有关PyCharm安装库失败的问题的解决方法
2020/02/02 Python
PyTorch预训练Bert模型的示例
2020/11/17 Python
浏览器实现移动端高性能css3动画(开启gpu加速)
2013/12/23 HTML / CSS
国外平面设计素材网站:The Hungry JPEG
2017/03/28 全球购物
财务会计大学生自我评价
2014/04/09 职场文书
留学顾问岗位职责
2014/04/14 职场文书
司法工作人员群众路线对照检查材料思想汇报
2014/09/30 职场文书
2014年卫生院工作总结
2014/12/03 职场文书
晚会闭幕词
2015/01/28 职场文书
小学生学习保证书
2015/02/26 职场文书
疑《守望先锋2》A测截图泄露 或将推出新模式、新界面
2022/04/03 其他游戏
利用Python脚本写端口扫描器socket,python-nmap
2022/07/23 Python