简体   繁体   English

等号后打印字符串以登录Python吗?

[英]Print string after equals to sign in Python?

I have lots of lines in a text file. 我在文本文件中有很多行。 One line for example: 例如一行:

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

Can somebody please tell me how to print all the string after the equals to sign ("="). 有人可以告诉我如何在等于号(“ =”)之后打印所有字符串。 For instance, in the above case, output should be "GaussianDistribution(0.28, 0.09)". 例如,在上述情况下,输出应为“ GaussianDistribution(0.28,0.09)”。

I tried to split the line and print the last index, however, it gives me "0.09)" answer, which is, of course, incorrect. 我试图拆分行并打印最后一个索引,但是,它给了我“ 0.09)”的答案,这当然是不正确的。

You don't need a regex, just split() it: 您不需要正则表达式,只需对它进行split()

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

or: 要么:

>>> s.split("=")[1].strip()
'GaussianDistribution(0.28, 0.09)'

You can use str.partition() : 您可以使用str.partition()

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

This is useful incase the data you need has another equals sign in it. 万一您需要的数据中有另一个等号,这很有用。

You can also use this: 您还可以使用以下命令:

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'

and of course: 而且当然:

>>> s = 'abcd=efgh'                       
>>> GetPart(s,0)                          #first part
>>> 'abcd'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM