![](/img/trans.png)
[英]Insert newline after equals sign in self documenting f-string in python3.8
[英]Print string after equals to sign in Python?
我在文本文件中有很多行。 例如一行:
838: DEBUG, GD, Parameter(Player_Appearance_Model) = GaussianDistribution(0.28, 0.09)
有人可以告訴我如何在等於號(“ =”)之后打印所有字符串。 例如,在上述情況下,輸出應為“ GaussianDistribution(0.28,0.09)”。
我試圖拆分行並打印最后一個索引,但是,它給了我“ 0.09)”的答案,這當然是不正確的。
您不需要正則表達式,只需對它進行split()
:
>>> s = "838: DEBUG, GD, Parameter(Player_Appearance_Model) = GaussianDistribution(0.28, 0.09)"
>>> s.split(" = ")[1]
'GaussianDistribution(0.28, 0.09)'
要么:
>>> s.split("=")[1].strip()
'GaussianDistribution(0.28, 0.09)'
您可以使用str.partition()
:
>>> s = "838: DEBUG, GD, Parameter(Player_Appearance_Model) = GaussianDistribution(0.28, 0.09)"
>>> print s.partition('= ')[2]
GaussianDistribution(0.28, 0.09)
萬一您需要的數據中有另一個等號,這很有用。
您還可以使用以下命令:
def GetPart(s,part=1):
out = s.split('=')[part].strip() #only '=', spaces will be removed
return out
>>> s = 'abcd=efgh'
>>> GetPart(s)
>>> 'efgh'
>>> s = 'abcd= efgh' #note extra spaces
>>> GetPart(s)
>>> 'efgh'
>>> s = 'abcd = efgh ' #even more space before/after
>>> GetPart(s)
>>> 'efgh'
而且當然:
>>> s = 'abcd=efgh'
>>> GetPart(s,0) #first part
>>> 'abcd'
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.