解決python3插入mysql時(shí)內(nèi)容帶有引號的問題
插入mysql時(shí),如果內(nèi)容中有引號等特殊符號,會報(bào)錯(cuò),
解決方法可以用反斜杠轉(zhuǎn)義,還可以用pymysql的一個(gè)方法自動轉(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è)。 </p>
<p> 此次IPO百世集團(tuán)一共發(fā)行4500萬股美國存托股份(ADS),每股價(jià)格為10美元,總?cè)谫Y額高達(dá)4.5億美元,為今年目前為止在美國上市的中國公司中募資規(guī)模最大的IPO。此外,百世和售股股東還允許其承銷商通過超額配售權(quán)購買額外不多于675萬股ADS。</p>
<p> 有中通這個(gè)“珠玉”在前,美股市場似'''
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í)有單引號, 雙引號處理方式 !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í)有雙引號和單引號("", ''),比如{'abc': '123', "def": "456"},
則會在execute(insert_cmd)時(shí)報(bào)錯(cuò)。
如果想保持?jǐn)?shù)據(jù)原始性,不使用replace替換成統(tǒng)一的單引號或者雙引號,
則可以使用!r來調(diào)用repr() 函數(shù), 將對象轉(zhuǎn)化為供解釋器讀取的形式。
repr() 返回一個(gè)對象的 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)容帶有引號的問題就是小編分享給大家的全部內(nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
python RabbitMQ 使用詳細(xì)介紹(小結(jié))
這篇文章主要介紹了python RabbitMQ 使用詳細(xì)介紹(小結(jié)),詳細(xì)的介紹了RabbitMQ的概念以及使用,對學(xué)習(xí)RabbitMQ有一定的幫助,非常具有實(shí)用價(jià)值,需要的朋友可以參考下2018-11-11Pytorch自動求導(dǎo)函數(shù)詳解流程以及與TensorFlow搭建網(wǎng)絡(luò)的對比
PyTorch是一個(gè)開源的Python機(jī)器學(xué)習(xí)庫,基于Torch,用于自然語言處理等應(yīng)用程序。2017年1月,由Facebook人工智能研究院(FAIR)基于Torch推出了PyTorch,這篇文章主要介紹了Pytorch自定義自動求導(dǎo)函數(shù),以及PyTorch與TensorFlow搭建網(wǎng)絡(luò)的對比2021-11-11python中使用正則表達(dá)式的后向搜索肯定模式(推薦)
這篇文章主要介紹了python里使用正則表達(dá)式的后向搜索肯定模式,本文通過代碼介紹的非常詳細(xì),包括語法介紹,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下2017-11-11python argparse 模塊命令行參數(shù)用法及說明
這篇文章主要介紹了python argparse 模塊命令行參數(shù)用法及說明,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-11-11