Python/Django: How to remove extra white spaces & tabs from a string?

后端 未结 6 2166
天命终不由人
天命终不由人 2020-12-29 21:59

I\'m building a website with Python/Django. Users submit tags. Each tag can contain multiple words. Each tag has an ID number. I want to make sure tags that are formatted sl

6条回答
  •  误落风尘
    2020-12-29 22:45

    This function removes everything which is not digit in a string. I use it all over the place.

    def parseInt(string):
        if isinstance(string, (str, int, unicode)):
            try:
                digit = int(''.join([x for x in string if x.isdigit() ]))
            except ValueError:
                return False
            else:
                return digit
        else:
            return False   
    

提交回复
热议问题