Convert, or unformat, a string to variables (like format(), but in reverse) in Python

前端 未结 7 2332
[愿得一人]
[愿得一人] 2020-12-15 03:44

I have strings of the form Version 1.4.0\\n and Version 1.15.6\\n, and I\'d like a simple way of extracting the three numbers from them. I know I

相关标签:
7条回答
  • 2020-12-15 04:19

    This

    a, b, c = (int(i) for i in mystr.split()[1].split('.'))
    

    will give you int values for a, b and c

    >>> a
    1
    >>> b
    15
    >>> c
    6
    

    Depending on how regular or irregular, i.e., consistent, your number/version formats will be, you may want to consider the use of regular expressions, though if they will stay in this format, I would favor the simpler solution if it works for you.

    0 讨论(0)
提交回复
热议问题