![](/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.