parsing a line of text to get a specific number

前端 未结 3 1321
隐瞒了意图╮
隐瞒了意图╮ 2021-01-26 00:03

I have a line of text in the form \" some spaces variable = 7 = \'0x07\' some more data\"

I want to parse it and get the number 7 from \

3条回答
  •  一向
    一向 (楼主)
    2021-01-26 01:01

    I would use a simpler solution, avoiding regular expressions.

    Split on '=' and get the value at the position you expect

    text = 'some spaces variable = 7 = ...'
    if '=' in text:
        chunks = text.split('=')
        assignedval = chunks[1]#second value, 7
        print 'assigned value is', assignedval
    else:
        print 'no assignment in line'
    

提交回复
热议问题