Strip all non-numeric characters (except for “.”) from a string in Python

后端 未结 6 1490
死守一世寂寞
死守一世寂寞 2020-12-07 22:26

I\'ve got a pretty good working snippit of code, but I was wondering if anyone has any better suggestions on how to do this:

val = \'\'.join([c for c in val          


        
6条回答
  •  执笔经年
    2020-12-07 23:06

    A simple solution is to use regular expessions

    import re 
    re.sub("[^0-9^.]", "", data)
    

提交回复
热议问题