最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

在Python中實(shí)現(xiàn)替換字符串中的子串的示例

 更新時(shí)間:2018年10月31日 09:17:58   作者:杰瑞26  
今天小編就為大家分享一篇在Python中實(shí)現(xiàn)替換字符串中的子串的示例,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧

假如有個(gè)任務(wù): 給定一個(gè)字符串,通過查詢字典,來替換給定字符中的變量。如果使用通常的方法:

>>> "This is a %(var)s" % {"var":"dog"}
'This is a dog'
>>>

其實(shí)可以使用string.Template類來實(shí)現(xiàn)上面的替換

>>> from string import Template
>>> words = Template("This is $var")
>>> print(words.substitute({"var": "dog"})) # 通過字典的方式來傳參
This is dog
>>> print(words.substitute(var="dog"))   # 通過關(guān)鍵字方式來傳參
This is dog
>>>

在創(chuàng)建Template實(shí)例時(shí),在字符串格式中,可以使用兩個(gè)美元符來代替$,還可以用${}將 變量擴(kuò)起來,這樣的話,變量后面還可以接其他字符或數(shù)字,這個(gè)使用方式很像Shell或者Perl里面的語言。下面以letter模板來示例一下:

>>> from string import Template
>>> letter = """Dear $customer,
... I hope you are having a great time!
... If you do not find Room $room to your satisfaction, let us know.
... Please accept this $$5 coupon.
...     Sincerely,
...     $manager,
...     ${name}Inn"""
>>> template = Template(letter)
>>> letter_dict = {"name": "Sleepy", "customer": "Fred Smith", "manager": "Tom Smith", "room": 308}
>>> print(template.substitute(letter_dict))
Dear Fred Smith,
I hope you are having a great time!
If you do not find Room 308 to your satisfaction, let us know.
Please accept this $5 coupon.
    Sincerely,
    Tom Smith,
    SleepyInn
>>>

有時(shí)候,為了給substitute準(zhǔn)備一個(gè)字典做參數(shù),最簡單的方法是設(shè)定一些本地變量,然后將這些變量交給local()(此函數(shù)創(chuàng)建一個(gè)字典,字典中的key就是本地變量,本地變量的值通過key來訪問)。

>>> locals()   # 剛進(jìn)入時(shí),沒有其他變量
{'__builtins__': <module '__builtin__' (built-in)>, '__name__': '__main__', '__doc__': None, '__package__': None}
>>> name = "Alice" # 創(chuàng)建本地變量name 
>>> age = 18   # 創(chuàng)建本地變量age
>>> locals()   # 再執(zhí)行l(wèi)ocals()函數(shù)就可以看到name, age的鍵值隊(duì)
{'name': 'Alice', '__builtins__': <module '__builtin__' (built-in)>, 'age': 18, '__package__': None, '__name__': '__mai
__', '__doc__': None}
>>> locals()["name"] # 通過鍵name來獲取值
'Alice'
>>> locals()["age"] # 通過鍵age來獲取值
18
>>>

有了上面的例子打底來看一個(gè)示例:

>>> from string import Template
>>> msg = Template("The square of $number is $square")
>>> for number in range(10):
...  square = number * number
...  print msg.substitute(locals())
...
The square of 0 is 0
The square of 1 is 1
The square of 2 is 4
The square of 3 is 9

另外一種方法是使用關(guān)鍵字參數(shù)語法而非字典,直接將值傳遞給substitute。

>>> from string import Template
>>> msg = Template("The square of $number is $square")
>>> for i in range(4):
...  print msg.substitute(number=i, square=i*i)
...
The square of 0 is 0
The square of 1 is 1
The square of 2 is 4
The square of 3 is 9
>>>

甚至可以同時(shí)傳遞字典和關(guān)鍵字

>>> from string import Template
>>> msg = Template("The square of $number is $square")
>>> for number in range(4):
...  print msg.substitute(locals(), square=number*number)
...
The square of 0 is 0
The square of 1 is 1
The square of 2 is 4
The square of 3 is 9
>>>

為了防止字典的條目和關(guān)鍵字參數(shù)顯示傳遞的值發(fā)生沖突,關(guān)鍵字參數(shù)優(yōu)先,比如:

>>> from string import Template
>>> msg = Template("It is $adj $msg")
>>> adj = "interesting"
>>> print(msg.substitute(locals(), msg="message"))
It is interesting message
 

以上這篇在Python中實(shí)現(xiàn)替換字符串中的子串的示例就是小編分享給大家的全部內(nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。

相關(guān)文章

  • python字符串,元組,列表,字典互轉(zhuǎn)代碼實(shí)例詳解

    python字符串,元組,列表,字典互轉(zhuǎn)代碼實(shí)例詳解

    這篇文章主要介紹了python字符串,元組,列表,字典互轉(zhuǎn)代碼實(shí)例詳解,需要的朋友可以參考下
    2020-02-02
  • 關(guān)于python中range()的參數(shù)問題

    關(guān)于python中range()的參數(shù)問題

    這篇文章主要介紹了關(guān)于python中range()的參數(shù)問題,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-05-05
  • Python制作基礎(chǔ)學(xué)生信息管理系統(tǒng)

    Python制作基礎(chǔ)學(xué)生信息管理系統(tǒng)

    本文詳細(xì)講解了Python制作基礎(chǔ)學(xué)生信息管理系統(tǒng)的實(shí)現(xiàn),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2021-12-12
  • Python AutoCAD 系統(tǒng)設(shè)置的實(shí)現(xiàn)方法

    Python AutoCAD 系統(tǒng)設(shè)置的實(shí)現(xiàn)方法

    這篇文章主要介紹了Python AutoCAD 系統(tǒng)設(shè)置的實(shí)現(xiàn)方法,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-04-04
  • python出現(xiàn)更新庫失敗A?new?release?of?pip?is?available:?23.0.1?->?23.3解決辦法

    python出現(xiàn)更新庫失敗A?new?release?of?pip?is?available:?23.0.

    學(xué)習(xí)了Python我們知道它自帶了很多的庫,同時(shí)我們還需要對某個(gè)庫進(jìn)行升級(jí),這篇文章主要給大家介紹了關(guān)于python出現(xiàn)更新庫失敗A?new?release?of?pip?is?available:?23.0.1?->?23.3的解決辦法,需要的朋友可以參考下
    2024-03-03
  • Python如何通過ARIMA模型進(jìn)行時(shí)間序列分析預(yù)測

    Python如何通過ARIMA模型進(jìn)行時(shí)間序列分析預(yù)測

    這篇文章主要介紹了Python如何通過ARIMA模型進(jìn)行時(shí)間序列分析預(yù)測問題,具有很好的參考價(jià)值,希望對大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2024-02-02
  • 深入學(xué)習(xí)Python中的上下文管理器與else塊

    深入學(xué)習(xí)Python中的上下文管理器與else塊

    這篇文章主要給大家介紹了關(guān)于Python中上下文管理器與else塊的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧。
    2017-08-08
  • 聊聊PyTorch中eval和no_grad的關(guān)系

    聊聊PyTorch中eval和no_grad的關(guān)系

    這篇文章主要介紹了聊聊PyTorch中eval和no_grad的關(guān)系,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2021-05-05
  • Python安裝jieba庫詳細(xì)教程

    Python安裝jieba庫詳細(xì)教程

    jieba庫是一款優(yōu)秀的 Python 第三方中文分詞庫,jieba 支持三種分詞模式:精確模式、全模式和搜索引擎模式,這篇文章主要介紹了Python安裝jieba庫教程,需要的朋友可以參考下
    2023-03-03
  • python中的數(shù)據(jù)結(jié)構(gòu)比較

    python中的數(shù)據(jù)結(jié)構(gòu)比較

    這篇文章主要介紹了python中的數(shù)據(jù)結(jié)構(gòu)比較,本文給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2019-05-05

最新評(píng)論

海南省| 方正县| 长宁县| 清远市| 台北县| 平阴县| 长丰县| 怀远县| 巩留县| 荆门市| 宣武区| 景德镇市| 遵化市| 安康市| 台安县| 买车| 苍山县| 长顺县| 任丘市| 怀宁县| 湖口县| 茂名市| 扶绥县| 金阳县| 花垣县| 全州县| 安岳县| 咸宁市| 南汇区| 敖汉旗| 黄龙县| 林芝县| 平果县| 镇康县| 灯塔市| 平南县| 滨海县| 景宁| 郎溪县| 洛南县| 大港区|