Python: How to remove [' and ']?

前端 未结 4 692
说谎
说谎 2021-01-29 11:36

I want to remove [\' from start and \'] characters from the end of a string.

This is my text:

\"[\'45453656565\']\"
         


        
4条回答
  •  广开言路
    2021-01-29 12:08

    You could loop over the character filtering if the element is a digit:

    >>> number_array =  "['34325235235']"
    >>> int(''.join(c for c in number_array if c.isdigit()))
    34325235235
    

    This solution works even for both "['34325235235']" and '["34325235235"]' and whatever other combination of number and characters.

    You also can import a package and use a regular expresion to get it:

    >>> import re
    >>> theString = "['34325235235']"
    >>> int(re.sub(r'\D', '', theString)) # Optionally parse to int
    

提交回复
热议问题