Read a list from txt file in Python

前端 未结 1 1286
臣服心动
臣服心动 2021-01-24 16:36

I need to read a list from a txt file and import as a list variable into my .py file. I could use the import function, but the program must be compiled and when it\'s compiled t

相关标签:
1条回答
  • 2021-01-24 17:13

    try to use ast.literal_eval :

    >>> a = str([['preset1','a1','b1'],['preset2','a2','b2'],['preset3','a3','b3'],['preset4','a4','b4']])
    >>> a
    "[['preset1', 'a1', 'b1'], ['preset2', 'a2', 'b2'], ['preset3', 'a3', 'b3'], ['preset4', 'a4', 'b4']]"
    >>> import ast
    >>> ast.literal_eval(a)
    [['preset1', 'a1', 'b1'], ['preset2', 'a2', 'b2'], ['preset3', 'a3', 'b3'], ['preset4', 'a4', 'b4']]
    >>> 
    
    0 讨论(0)
提交回复
热议问题