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.
I am trying to a save a list into a file in a way
Share
If your data is only a list whose items are basic types (e.g. 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 e.g.