简体   繁体   中英

Alternate ways of saving a list into a file in Python

I am trying to a save a list into a file in a way that when I load and read the file again I get my lists as they are. In other words,the datatype doesn't change while saving and loading. Because right now, I use "write" to save my list into a file, and when I try to load it back into memory I get strings rather than real lists. is there a way to convert them back to lists after loading? or should I change the way I save my lists into a file.Please note that I don't want to use Pickle. Thanks EDIT: my problem with pickle is that I have to add my lists step by step in different part of the code.Thus, I don't have all the lists at once so I can pickle them. This is the problem that I had.It gives me wrong answer, I guess it is because pickle requires all the info in one place and adds them to file at once. (?) I have only integers in my lists.

If your data is only a list whose items are basic types (eg str, unicode, int, float) and lists or dicts whose elements are etc etc, then you can use json ; this is portable across languages (is that your problem with pickle?).

Update after question edited """my problem with pickle is that I have to add my lists step by step in different part of the code"""

Have you considered gathering the lists to be pickled as you find them and then pickling all of them at once at the end? Same applies with json etc. All you need is a container to keep your lists in. You can make this look nicer by putting it in a class eg

class Preserver(object):
    def __init__(self):
        self._bottle = []
    def add(self, an_object):
        self._bottle.append(an_object)
    def preserve(self, filepath):
        # code using pickle or json to push self._bottle
        # out to a file named "filepath"

If you trust the input, use read the file in and use eval :

>>> a_list = [1, 3, 5]
>>> with open('test.txt', 'w') as f:
...     f.write(str(a_list))
... 
>>> with open('test.txt') as f:
...     read_list = eval(f.readlines()[0])
... 
>>> read_list
[1, 3, 5]

You could also use ast.literal_eval (python 2.6+) which is safer to use than my eval recommendation.

From the docs:

Safely evaluate an expression node or a string containing a Python expression. The string or node provided may only consist of the following Python literal structures: strings, numbers, tuples, lists, dicts, booleans, and None.

This can be used for safely evaluating strings containing Python expressions from untrusted sources without the need to parse the values oneself.

And for example:

>>> import ast
>>> a_list = [1, 3, 5]
>>> with open('test.txt', 'w') as f:
...     f.write(repr(a_list))
... 
>>> with open('test.txt') as f:
...     read_list = ast.literal_eval(f.readlines()[0])
... 
>>> read_list
[1, 3, 5]

您应该尝试pyyaml添加的奖励,它确实是人类可读的文本文件。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM