How to turn a 'string list' into a real list?

后端 未结 4 1530
花落未央
花落未央 2021-01-25 19:42

I am opening a .txt file and have to use a list inside of it for a function I am writing. This is one of the lists given in the text file:

\'[24, 72         


        
4条回答
  •  独厮守ぢ
    2021-01-25 20:41

    you don't need to go importing anything for this

    s = '[24, 72, 95, 100, 59, 80, 87]'

    s.translate(None, '[]').split(', ') will give you a list of numbers that are strings

    if you want a list of ints try

    [int(i) for i in s.translate(None, '[]').split(', ')]

提交回复
热议问题