是否在Python中的equals后面打印字符串?

2024-06-26 00:15:51 发布

您现在位置:Python中文网/ 问答频道 /正文

我在一个文本文件中有很多行。例如一行:

838: DEBUG, GD, Parameter(Player_Appearance_Model) = GaussianDistribution(0.28, 0.09)

有人能告诉我如何打印等号(“=”)后面的所有字符串吗。例如,在上面的例子中,输出应该是“GaussianDistribution(0.28,0.09)”。在

我试着把这行分开打印最后一个索引,但是它给我的答案是“0.09”,这当然是不正确的。在


Tags: 字符串答案debugmodelparameter例子player文本文件
3条回答

您不需要regex,只要split()它:

>>> s = "838: DEBUG, GD, Parameter(Player_Appearance_Model) = GaussianDistribution(0.28, 0.09)"
>>> s.split(" = ")[1]
'GaussianDistribution(0.28, 0.09)'

或者:

^{pr2}$

您也可以使用此选项:

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'

当然还有:

^{pr2}$

您可以使用^{}

>>> s = "838: DEBUG, GD, Parameter(Player_Appearance_Model) = GaussianDistribution(0.28, 0.09)"
>>> print s.partition('= ')[2]
GaussianDistribution(0.28, 0.09)

这在您需要的数据中有另一个等号时很有用。在

相关问题 更多 >