Python equivalent to atoi / atof

后端 未结 6 2046
無奈伤痛
無奈伤痛 2020-12-15 20:48

Python loves raising exceptions, which is usually great. But I\'m facing some strings I desperately want to convert to integers using C\'s atoi / atof semantics - e.g. atoi

6条回答
  •  我在风中等你
    2020-12-15 21:26

    Perhaps use a quick regex to grab only the first part of the string that can be considered numeric? Something like...

    -?[0-9]+(?:\.[0-9]+)?
    

    for floats and for ints just,

    -?[0-9]+
    

提交回复
热议问题