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

后端 未结 4 486
无人及你
无人及你 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条回答
  •  醉话见心
    2020-12-19 07:34

    You could just pickle your data instead. To save it:

    >>> import pickle
    >>> p= [[123231,2345,888754],[223467,85645]]  
    >>> with open("data.txt", "wb") as internal_filename:
    ...     pickle.dump(p, internal_filename)
    

    To load it:

    >>> with open("data.txt", "rb") as new_filename:
    ...     pp = pickle.load(new_filename)
    >>> pp
    [[123231, 2345, 888754], [223467, 85645]]
    

    This is also useful for much more elaborate objects.

提交回复
热议问题