解決python3插入mysql時內容帶有引號的問題
插入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 rel="external nofollow" class="keyword f_st" target="_blank">中通</a></span><span id=quote_ZTO></span>快遞之后第二家赴美上市的快遞物流企業(yè)。 </p>
<p> 此次IPO百世集團一共發(fā)行4500萬股美國存托股份(ADS),每股價格為10美元,總融資額高達4.5億美元,為今年目前為止在美國上市的中國公司中募資規(guī)模最大的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中執(zhí)行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)時報錯。
如果想保持數(shù)據(jù)原始性,不使用replace替換成統(tǒng)一的單引號或者雙引號,
則可以使用!r來調用repr() 函數(shù), 將對象轉化為供解釋器讀取的形式。
repr() 返回一個對象的 string 格式。
!r 表示使用repr()替代默認的str()來返回。
注:repr是str的方法,所以value需要是string,若數(shù)據(jù)是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 DataFrame一列拆成多列以及一行拆成多行
這篇文章主要介紹了Python DataFrame一列拆成多列以及一行拆成多行,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2019-08-08
Django集成搜索引擎Elasticserach的方法示例
這篇文章主要介紹了Django集成搜索引擎Elasticserach的方法示例,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2019-06-06
通過selenium抓取某東的TT購買記錄并分析趨勢過程解析
這篇文章主要介紹了通過selenium抓取某東的TT購買記錄并分析趨勢過程解析,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下2019-08-08
Python入門教程(三十九)Python的NumPy安裝與入門
這篇文章主要介紹了Python入門教程(三十九)Python的NumPy安裝與入門,NumPy 是一個Python包,它是一個由多維數(shù)組對象和用于處理數(shù)組的例程集合組成的庫,,需要的朋友可以參考下2023-05-05
python自動統(tǒng)計zabbix系統(tǒng)監(jiān)控覆蓋率的示例代碼
這篇文章主要介紹了python自動統(tǒng)計zabbix系統(tǒng)監(jiān)控覆蓋率的示例代碼,本文給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2021-04-04
Python cx_freeze打包工具處理問題思路及解決辦法
這篇文章主要介紹了Python cx_freeze打包工具處理問題思路及解決辦法的相關資料,需要的朋友可以參考下2016-02-02

