Extracting only characters from a string in Python

前端 未结 7 1186
后悔当初
后悔当初 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 07:56

    What about doing this?

    >>> import ast
    >>> " ".join([k[0] for k in ast.literal_eval("{('players',): 24, ('year',): 28, ('money',): 19, ('ipod',): 36, ('case',): 23, ('mini',): 46}").keys()])
    'case mini year money ipod players'
    

提交回复
热议问题