解决python3插入mysql时内容带有引号的问题

作者:ezreal is easy 时间:2024-01-24 13:34:09 

插入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>快递之后第二家赴美上市的快递物流企业。&nbsp;</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.

来源:https://blog.csdn.net/lk7688535/article/details/78053938

标签:python3,mysql,内容,引号
0
投稿

猜你喜欢

  • Python的Flask框架中使用Flask-Migrate扩展迁移数据库的教程

    2024-01-19 06:38:45
  • 请问能否在ASP中调用DLL

    2009-06-07 18:24:00
  • Python+OpenCV人脸识别签到考勤系统实现(附demo)

    2022-01-25 14:05:50
  • pycharm 创建py文件总是为txt格式的问题及解决

    2022-01-13 16:03:27
  • 关于通过Java连接mysql对反斜杠”\\“转义的测试详解

    2024-01-27 06:52:59
  • python实现图像检索的三种(直方图/OpenCV/哈希法)

    2021-08-11 17:15:08
  • Tensorflow 实现分批量读取数据

    2023-09-23 23:04:44
  • python登陆asp网站页面的实现代码

    2021-01-20 00:03:31
  • JavaScript控制台的更多功能

    2024-02-24 12:46:42
  • Python中输出ASCII大文字、艺术字、字符字小技巧

    2021-03-03 00:06:49
  • 大数据量,海量数据处理方法总结

    2024-01-12 21:59:38
  • SQLite3中文编码 Python的实现

    2023-02-03 13:21:38
  • 多级联动下拉选择框,动态获取下一级

    2008-09-04 10:34:00
  • Python实现读取txt文件并转换为excel的方法示例

    2023-07-25 15:20:16
  • python函数的重新定义及练习

    2023-10-12 22:47:55
  • asp的系统变量ServerVariables (“HTTP_USER_AGENT“)问题

    2009-02-04 15:51:00
  • 利用Python 实现分布式计算

    2021-12-21 22:59:26
  • python fabric实现远程操作和部署示例

    2021-07-18 14:22:14
  • Python 类属性与实例属性,类对象与实例对象用法分析

    2023-03-12 01:43:03
  • 基于mysq字段选择的详解

    2024-01-23 20:11:20
  • asp之家 网络编程 m.aspxhome.com