Get only numbers from string in python

后端 未结 8 1217
感动是毒
感动是毒 2020-12-06 18:37

I want to get only the numbers from a string. For example I have something like this

just=\'Standard Price:20000\'

And I only want it to pr

8条回答
  •  陌清茗
    陌清茗 (楼主)
    2020-12-06 18:42

    A more crude way if you don't want to use regex is to make use of slicing. Please remember that this would work for extracting any number if the structure of the text remains the same.

    just = 'Standard Price:20000'
    reqChar = len(just) - len('Standard Price:')
    print(int(just[-reqChar:]))
    >> 20000
    

提交回复
热议问题