溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

解決python3插入mysql時(shí)內(nèi)容帶有引號(hào)的問題

發(fā)布時(shí)間:2020-10-07 08:28:44 來源:腳本之家 閱讀:292 作者:ezreal is easy 欄目:開發(fā)技術(shù)

插入mysql時(shí),如果內(nèi)容中有引號(hào)等特殊符號(hào),會(huì)報(bào)錯(cuò),

解決方法可以用反斜杠轉(zhuǎn)義,還可以用pymysql的一個(gè)方法自動(dòng)轉(zhuǎn)義:

c = '''  北京時(shí)間9月20日晚間9點(diǎn)半,智能供應(yīng)鏈服務(wù)供應(yīng)商百世集團(tuán)將在<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è)。&nbsp;</p>
<p>  此次IPO百世集團(tuán)一共發(fā)行4500萬股美國(guó)存托股份(ADS),每股價(jià)格為10美元,總?cè)谫Y額高達(dá)4.5億美元,為今年目前為止在美國(guó)上市的中國(guó)公司中募資規(guī)模最大的IPO。此外,百世和售股股東還允許其承銷商通過超額配售權(quán)購買額外不多于675萬股ADS。</p>
<p>  有中通這個(gè)“珠玉”在前,美股市場(chǎng)似'''

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)

補(bǔ)充拓展:Python中執(zhí)行MySQL語句, 遇到同時(shí)有單引號(hào), 雙引號(hào)處理方式 !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} 作用是設(shè)置字段的值,一般情況應(yīng)該是:

{0}='{1}'.format(columnA, value)

但若value中同時(shí)有雙引號(hào)和單引號(hào)("", ''),比如{'abc': '123', "def": "456"},

則會(huì)在execute(insert_cmd)時(shí)報(bào)錯(cuò)。

如果想保持?jǐn)?shù)據(jù)原始性,不使用replace替換成統(tǒng)一的單引號(hào)或者雙引號(hào),

則可以使用!r來調(diào)用repr() 函數(shù), 將對(duì)象轉(zhuǎn)化為供解釋器讀取的形式。

repr() 返回一個(gè)對(duì)象的 string 格式。

!r 表示使用repr()替代默認(rèn)的str()來返回。

注:repr是str的方法,所以value需要是string,若數(shù)據(jù)是dict等類型,需要使用str()轉(zhuǎn)換成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時(shí)內(nèi)容帶有引號(hào)的問題就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持億速云。

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI