How to extract integers from a string separated by spaces in Python 2.7?

后端 未结 4 2123
南笙
南笙 2021-01-05 16:25

I want to extract integers from a string in which integers are separated by blank spaces i.e \' \'. How could I do that ??

Input

I=\'1 15 163 132\'
<         


        
4条回答
  •  梦毁少年i
    2021-01-05 17:09

    import re
    x="1 15 163 132"
    print re.findall(r"[+-]?\d+(?:\.\d+)?",x)
    

    If you have only positive integers you can simply do

    print re.findall(r"\d+",x)
    
    [1, 15, 163, 132]
    

提交回复
热议问题