可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
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 changed list as a list type?
I have tried:
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:
score = [line.rstrip('\n') for line in f]
print(score)
But this results in the elements in the list being strings not integers.
回答1:
You can use pickle
module for that.
This module have two methods,
- Pickling(dump): Convert Python objects into string representation.
- Unpickling(load): Retrieving original objects from stored string representstion.
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]
回答2:
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.
回答3:
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'
回答4:
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']
回答5:
pickle
and other serialization packages work. So does writing it to a .py
file that you can then import.
>>> score = [1,2,3,4,5]
>>>
>>> with open('file.py', 'w') as f:
... f.write('score = %s' % score)
...
>>> from file import score as my_list
>>> print(my_list)
[1, 2, 3, 4, 5]
回答6:
I am using pandas.
import pandas as pd
x = pd.Series([1,2,3,4,5])
x.to_excel('temp.xlsx')
y = list(pd.read_excel('temp.xlsx')[0])
print(y)
Use this if you are anyway importing pandas for other computations.
回答7:
What I did not like with many answers is that it makes way too many system calls by writing to the file line per line. Imho it is best to join list with '\n' (line return) and then write it only once to the file:
mylist = ["abc", "def", "ghi"]
myfile = "file.txt"
with open(myfile, 'w') as f:
f.write("\n".join(mylist))
and then to open it and get your list again:
with open(myfile, 'r') as f:
mystring = f.read()
my_list = mystring.split("\n")
回答8:
You can also use 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 more
# 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 advantage of using a json is that:
- The file is human readable
- Non-python programs can read and understand the json file
- You can literally save any list/dictionary in python to a json (as long as none of the values are objects/functions)
Typically, you'll see jsons used for more complicated nested lists/dictionaries (like an array of records). The main disadvantage of storing your data as a json is that the data is stored in plain-text, and as such is completely uncompressed, making it a slow and bloated option for large amounts of data.