partition string in python and get value of last segment after colon

后端 未结 4 1588
别跟我提以往
别跟我提以往 2020-12-14 05:30

I need to get the value after the last colon in this example 1234567

client:user:username:type:1234567

I don\'t need anything else from the

相关标签:
4条回答
  • 2020-12-14 06:11

    Use this:

    "client:user:username:type:1234567".split(":")[-1]
    
    0 讨论(0)
  • 2020-12-14 06:23

    You could also use pygrok.

    from pygrok import Grok
    text = "client:user:username:type:1234567"
    pattern = """%{BASE10NUM:type}"""
    grok = Grok(pattern)
    print(grok.match(text))
    

    returns

    {'type': '1234567'}
    
    0 讨论(0)
  • 2020-12-14 06:25
    result = mystring.rpartition(':')[2]
    

    If you string does not have any :, the result will contain the original string.

    An alternative that is supposed to be a little bit slower is:

    result = mystring.split(':')[-1]
    
    0 讨论(0)
  • 2020-12-14 06:26
    foo = "client:user:username:type:1234567"
    last = foo.split(':')[-1]
    
    0 讨论(0)
提交回复
热议问题