Reading a list of lists from a file as list of lists in python

后端 未结 4 482
无人及你
无人及你 2020-12-19 06:49

I collected data in the form of list of lists and wrote the data into a text file. The data in the text file looks like

[[123231,2345,888754],[223467,85645]         


        
4条回答
  •  梦毁少年i
    2020-12-19 07:36

    Also you can use eval to achieve this, but the other solutions maybe better:

    # reading a list from a file
    f = open('data.txt', 'r')
    l = eval(f.read())
    
    # storing a list to a file
    outf = open('out','w')
    outf.write(str(l))
    

    Sometime using eval is a bad practice. For more information check this post.

提交回复
热议问题