2013-08-28 27 views
1

我在文本文件中有很多行。一行例如:等于在Python中登录后打印字符串?

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

有人可以请告诉我如何打印等号后面的所有字符串(“=”)。例如,在上述情况下,输出应该是“GaussianDistribution(0.28,0.09)”。

我试图分割线和打印最后一个索引,但是,它给了我“0.09)”的答案,当然,这是不正确的。

+0

你等号或逗号分割?用等号分割应该给你正确的结果。 – Jerry

+0

如果字符串包含多个'='会怎么样? –

+0

@AshwiniChaudhary,幸运的是它不包含任何倍数= – Sanchit

回答

7

你并不需要一个正则表达式,只是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)' 
3

您可以使用str.partition()

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

这是柜面的数据有用你需要有另一个等号。

0

您也可以使用此:

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'