Python中eval()函數(shù)的功能及使用方法小結(jié)
可以把list,tuple,dict和string相互轉(zhuǎn)化。
1、字符串轉(zhuǎn)換成列表
>>>a = "[[1,2], [3,4], [5,6], [7,8], [9,0]]" >>>type(a) >>> b = eval(a) >>> print b[[1, 2], [3, 4], [5, 6], [7, 8], [9, 0]] >>> type(b)
2、字符串轉(zhuǎn)換成字典
>>> a = "{1: 'a', 2: 'b'}"
>>> type(a)
>>> b = eval(a)
>>> print b{1: 'a', 2: 'b'}
>>> type(b)3、字符串轉(zhuǎn)換成元組
>>> a = "([1,2], [3,4], [5,6], [7,8], (9,0))" >>> type(a) >>> b = eval(a) >>> print b([1, 2], [3, 4], [5, 6], [7, 8], (9, 0)) >>> type(b)
4、eval()函數(shù)傳進(jìn)去的參數(shù)必須是字符串或者是對(duì)象
示例1:
>>>a=10;
>>>print(eval("a+1"))
>>>執(zhí)行結(jié)果: 11示例2:
>>>a=10;
>>>g={'a':4}
>>>print(eval("a+1",g))
>>>執(zhí)行結(jié)果:5示例3:
>>>a=10
>>>b=20
>>>c=30
>>>g={'a':6,'b':8}
>>>t={'b':100,'c':10}
>>>print(eval('a+b+c',g,t))
>>>執(zhí)行結(jié)果:116? (a=6, b=100, c=10)5、eval函數(shù),eval去除引號(hào)后會(huì)檢查到它是不是可計(jì)算的,如果可計(jì)算會(huì)將計(jì)算的結(jié)果打印出來,如果不可計(jì)算直接返回結(jié)果
示例1:不可計(jì)算》》》直接輸出結(jié)果
>>>s='["a","b","c"]' >>>print(eval(s)) >>>執(zhí)行結(jié)果:?['a', 'b', 'c']
示例2:表達(dá)式可計(jì)算》》》計(jì)算之后輸出結(jié)果
>>>s='abs(-10)'?? >>>print(eval(s)) >>>輸出結(jié)果:10?
5、eval()函數(shù),功能是將string變成算術(shù)表達(dá)式來執(zhí)行
示例1:
>>>eval("1+2")
>>>執(zhí)行結(jié)果: 3示例2:
>>> eval("print(\"hello, world\")")
>>>輸出結(jié)果:hello, worldeval(str)函數(shù)很強(qiáng)大,官方解釋為:將字符串str當(dāng)成有效的表達(dá)式來求值并返回計(jì)算結(jié)果。所以,結(jié)合math當(dāng)成一個(gè)計(jì)算器很好用。
eval()函數(shù)常見作用有:
1、計(jì)算字符串中有效的表達(dá)式,并返回結(jié)果
>>> eval('pow(2,2)') 4 >>> eval('2 + 2') 4 >>> eval("n + 4") 85
2、將字符串轉(zhuǎn)成相應(yīng)的對(duì)象(如list、tuple、dict和string之間的轉(zhuǎn)換)
將python內(nèi)存中的數(shù)據(jù)類型轉(zhuǎn)成其該有的數(shù)據(jù)類型,而不是json字符串
>>> a = "[[1,2], [3,4], [5,6], [7,8], [9,0]]"
>>> b = eval(a)
>>> b
[[1, 2], [3, 4], [5, 6], [7, 8], [9, 0]]
>>> a = "{1:'xx',2:'yy'}"
>>> c = eval(a)
>>> c
{1: 'xx', 2: 'yy'}
>>> a = "(1,2,3,4)"
>>> d = eval(a)
>>> d
(1, 2, 3, 4)例外:json格式的字典字符串如果鍵值對(duì)中的值為單字節(jié)的話可以使用eval()表達(dá)式轉(zhuǎn)換為python數(shù)據(jù)類型【例如json轉(zhuǎn)dict】
①但是如果json格式的字典字符串中的鍵值對(duì)中的值為復(fù)雜的數(shù)據(jù)類型如列表或者列表中嵌套字典則無法將json轉(zhuǎn)換為dict數(shù)據(jù)
②eval(str)當(dāng)字符串中有null【null的反序列化通過json.load()方法可以轉(zhuǎn)換為None】值,無法將字符串轉(zhuǎn)成相應(yīng)的對(duì)象,會(huì)報(bào)錯(cuò)。
json_string = """{
"answerList": [
{
"content": "string",
"questionId": "string",
"questionIndex": 0,
"questionTitle": "string",
"questionType": 0,
"questionWeight": 0,
"score": 0,
"scoreId": "string",
"scoreName": "string",
"scoreWeight": 0,
"selectOptions": [
{
"disabledWeight": true,
"evaluateStatus": 0,
"optionId": "string",
"optionTitle": "string",
"optionWeight": 0
}
],
"tags": [
{
"tagId": "string",
"tagTitle": "string"
}
]
}
],
"channel": "string",
"externalUnionid": "string",
"key": "string",
"qaJson": "string",
"unionid": "string",
"uuid": "string"
}"""
print(json_string) #打印json字符串
print(type(json_string))#打印json字符串的類型:str
print(eval(json_string))使用eval()表達(dá)式把json字符串轉(zhuǎn)換為python數(shù)據(jù)類型:字典類型--》結(jié)果失敗,報(bào)錯(cuò)
json_dict = json.loads(json_string) #使用json.loads反序列化方式,將json字符串轉(zhuǎn)換為python數(shù)據(jù)類型成功
print(type(json_dict)) #打印轉(zhuǎn)換后的數(shù)據(jù)類型:dict
print(json_dict)#打印轉(zhuǎn)換成功后的字典數(shù)據(jù)3、將利用反引號(hào)轉(zhuǎn)換的字符串再反轉(zhuǎn)回對(duì)象
>>> list1 = [1,2,3,4,5] >>> `list1` '[1, 2, 3, 4, 5]' >>> type(`list1`) <type 'str'> >>> type(eval(`list1`)) <type 'list'> >>> a = eval(`list1`) >>> a [1, 2, 3, 4, 5]
到此這篇關(guān)于Python中eval()函數(shù)的功能及使用方法的文章就介紹到這了,更多相關(guān)Python eval()內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- python中eval的用法及說明
- Python 中eval()函數(shù)的正確使用及其風(fēng)險(xiǎn)分析(使用示例)
- python中eval函數(shù)使用與異常處理詳解
- Python使用eval函數(shù)解析和執(zhí)行字符串
- Python中的eval()函數(shù)使用詳解
- python中的exec()、eval()及complie()示例詳解
- python中關(guān)于eval函數(shù)的使用及說明
- Python eval()與exec()函數(shù)使用介紹
- Python?eval()和exec()函數(shù)使用詳解
- Python eval函數(shù)的實(shí)現(xiàn)
相關(guān)文章
python try...finally...的實(shí)現(xiàn)方法
這篇文章主要介紹了python try...finally...的實(shí)現(xiàn)方法,幫助大家更好的理解和使用python,感興趣的朋友可以了解下2020-11-11
pyqt4教程之實(shí)現(xiàn)半透明的天氣預(yù)報(bào)界面示例
這篇文章主要介紹了pyqt4實(shí)現(xiàn)半透明的天氣預(yù)報(bào)界面示例,需要的朋友可以參考下2014-03-03
python過濾字符串中不屬于指定集合中字符的類實(shí)例
這篇文章主要介紹了python過濾字符串中不屬于指定集合中字符的類,涉及Python針對(duì)字符串與集合的相關(guān)操作技巧,需要的朋友可以參考下2015-06-06
20行python代碼實(shí)現(xiàn)人臉識(shí)別
這篇文章主要介紹了python人臉識(shí)別,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-05-05
Python實(shí)現(xiàn)杰卡德距離以及環(huán)比算法講解
這篇文章主要為大家介紹了Python實(shí)現(xiàn)杰卡德距離以及環(huán)比算法的示例講解,有需要的朋友可以借鑒參考下2022-02-02
python實(shí)現(xiàn)二叉查找樹實(shí)例代碼
這篇文章主要介紹了python實(shí)現(xiàn)二叉查找樹實(shí)例代碼,分享了相關(guān)代碼示例,小編覺得還是挺不錯(cuò)的,具有一定借鑒價(jià)值,需要的朋友可以參考下2018-02-02

