parsing a line of text to get a specific number

前端 未结 3 1313
隐瞒了意图╮
隐瞒了意图╮ 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:00

    Basic regex code snippet to find numbers in a string.

    >>> import re
    >>> input = " some spaces variable = 7 = '0x07' some more data"
    >>> nums = re.findall("[0-9]*", input)
    >>> nums = [i for i in nums if i]  # remove empty strings
    >>> nums
    ['7', '0', '07']
    

    Check out the documentation and How-To on python.org.

提交回复
热议问题