Say I have the list score=[1,2,3,4,5] and it gets changed whilst my program is running. How could I save it to a file so that next time the program is run I can access the c
If you want you can use numpy's save function to save the list as file. Say you have two lists
sampleList1=['z','x','a','b']
sampleList2=[[1,2],[4,5]]
here's the function to save the list as file, remember you need to keep the extension .npy
def saveList(myList,filename):
# the filename should mention the extension 'npy'
np.save(filename,myList)
print("Saved successfully!")
and here's the function to load the file into a list
def loadList(filename):
# the filename should mention the extension 'npy'
tempNumpyArray=np.load(filename)
return tempNumpyArray.tolist()
a working example
>>> saveList(sampleList1,'sampleList1.npy')
>>> Saved successfully!
>>> saveList(sampleList2,'sampleList2.npy')
>>> Saved successfully!
# loading the list now
>>> loadedList1=loadList('sampleList1.npy')
>>> loadedList2=loadList('sampleList2.npy')
>>> loadedList1==sampleList1
>>> True
>>> print(loadedList1,sampleList1)
>>> ['z', 'x', 'a', 'b'] ['z', 'x', 'a', 'b']
I decided I didn't want to use a pickle because I wanted to be able to open the text file and change its contents easily during testing. Therefore, I did this:
score = [1,2,3,4,5]
with open("file.txt", "w") as f:
for s in score:
f.write(str(s) +"\n")
with open("file.txt", "r") as f:
for line in f:
score.append(int(line.strip()))
So the items in the file are read as integers, despite being stored to the file as strings.
If you don't want to use pickle, you can store the list as text and then evaluate it:
data = [0,1,2,3,4,5]
with open("test.txt", "w") as file:
file.write(str(data))
with open("test.txt", "r") as file:
data2 = eval(file.readline())
# Let's see if data and types are same.
print(data, type(data), type(data[0]))
print(data2, type(data2), type(data2[0]))
[0, 1, 2, 3, 4, 5] class 'list' class 'int'
[0, 1, 2, 3, 4, 5] class 'list' class 'int'
You can use pickle
module for that.
This module have two methods,
https://docs.python.org/3.3/library/pickle.html
Code:
>>> import pickle
>>> l = [1,2,3,4]
>>> with open("test.txt", "wb") as fp: #Pickling
... pickle.dump(l, fp)
...
>>> with open("test.txt", "rb") as fp: # Unpickling
... b = pickle.load(fp)
...
>>> b
[1, 2, 3, 4]
Also Json
https://docs.python.org/3/library/json.html
Code:
>>> import json
>>> with open("test.txt", "w") as fp:
... json.dump(l, fp)
...
>>> with open("test.txt", "r") as fp:
... b = json.load(fp)
...
>>> b
[1, 2, 3, 4]
Although the accepted answer works, you should really be using python's json
module:
import json
score=[1,2,3,4,5]
with open("file.json", 'w') as f:
# indent=2 is not needed but makes the file
# human-readable for more complicated data
json.dump(score, f, indent=2)
with open("file.json", 'r') as f:
score = json.load(f)
print(score)
The main advantages of using a json are that:
The main disadvantages of using a json are that:
hdf5
is a much better candidate for that).The json format is used for all kinds of purposes:
In general, if I want to store something I know I'm only ever going to use in the context of a python program, I typically go to pickle
. If I need a platform agnostic solution, or I need to be able to inspect my data once it's stored, I go with json
.
errorlist = ['aaaa', 'bbbb', 'cccc', 'ffffdd']
f = open("filee.txt", "w")
f.writelines(nthstring + '\n' for nthstring in errorlist)
f = open("filee.txt", "r")
cont = f.read()
contentlist = cont.split()
print(contentlist)