如果我有幾個字串:
string1 = 'text here pri=1 more text urgent=True'
string2 = 'more text here pri=5 urgent=False'
string3 = 'another text pri=3 urgent=False'
string4 = 'more text pri=10 urgent=True'
如何從字串中獲取“pri”和“urgent”的值到變數?
我想出了一些解決方案,比如 pri 只是使用 str.find 定位它,然后向前跳轉 4 個變數,等等......
但必須有一種更有效的方式來閱讀它,我該怎么做?
uj5u.com熱心網友回復:
您可以使用 .split 函式來獲取所有不同的單詞。它會回傳一個串列。對于字串 1:
['text', 'here', 'pri=1', 'more', 'text', 'urgent=True']
現在您可以遍歷串列并搜索“=”。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/442242.html
