Extracting only characters from a string in Python

前端 未结 7 1163
后悔当初
后悔当初 2020-12-08 07:01

In Python, I want to extract only the characters from a string.

Consider I have the following string,

input = \"{(\'players\',): 24, (\'year\',): 28         


        
相关标签:
7条回答
  • 2020-12-08 08:01
    import re
    string = ''.join([i for i in re.findall('[\w +/.]', string) if i.isalpha()])
    
    #'[\w +/.]' -> it will give characters numbers and punctuation, then 'if i.isalpha()' this condition will only get alphabets out of it and then join list to get expected result.
    # It will remove spaces also.
    
    0 讨论(0)
提交回复
热议问题