Convert a string to a list of floats (in python)

后端 未结 5 1839
别那么骄傲
别那么骄傲 2021-01-29 09:51

For data storage purposes I am trying to recover lists of floats from a .txt file. From the string like:

a = \'[1.3, 2.3, 4.5]\'

I want to recover:<

5条回答
  •  说谎
    说谎 (楼主)
    2021-01-29 10:17

    Use the ast module.

    Ex

    import ast
    print(ast.literal_eval('[1.3, 2.3, 4.5]'))
    

    Output:

    [1.3, 2.3, 4.5]
    

提交回复
热议问题