python 過濾字母、標點等 保留數字


crazystring = ‘dade142.;!0142f[.,]ad’

只保留數字 >>> filter(str.isdigit, crazystring) ‘1420142’

只保留字母 >>> filter(str.isalpha, crazystring) ‘dadefad’

只保留字母和數字 >>> filter(str.isalnum, crazystring) ‘dade1420142fad’

如果想保留數字0-9和小數點’.’ 則需要自定義函數

filter(lambda ch: ch in ‘0123456789.’, crazystring) ‘142.0142.’

或者使用正則表達式或循環

http://stackoverflow.com/questions/947776/strip-all-non-numeric-characters-except-for-from-a-string-in-python


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM