Oce*_*nce 27 python file list pickle python-3.x
假设我有列表得分= [1,2,3,4,5],并且在我的程序运行时它会被更改.如何将其保存到文件中以便下次运行程序时我可以将更改后的列表作为列表类型进行访问?
我试过了:
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)
Run Code Online (Sandbox Code Playgroud)
但这会导致列表中的元素不是整数.
Viv*_*ble 52
您可以使用pickle 模块.这个模块有两种方法,
https://docs.python.org/3.3/library/pickle.html 代码:
>>> 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]
Run Code Online (Sandbox Code Playgroud)
Jay*_*ody 28
尽管接受的答案有效,但您确实应该使用 python 的json模块:
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
json.dump(score, f, indent=2)
with open("file.json", 'r') as f:
score = json.load(f)
print(score)
Run Code Online (Sandbox Code Playgroud)
优点:
json 是一种广泛采用的标准化数据格式,因此非python程序可以轻松读取和理解json文件json 文件是人类可读的json文件中(只要所有内容都是可序列化的)。缺点:
hdf5它的用途)。我应该使用哪一种?:
picklepickle.jsonjson的常见用例json:
node.js使用package.json文件来跟踪项目详细信息、依赖项、脚本等...)RESTAPI 用于json传输和接收数据csv,xml或yaml文件Oce*_*nce 18
我决定不想使用泡菜,因为我希望能够在测试期间轻松打开文本文件并更改其内容.因此,我这样做了:
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()))
Run Code Online (Sandbox Code Playgroud)
因此,文件中的项目作为整数读取,尽管作为字符串存储到文件中.
小智 13
如果您不想使用pickle,可以将列表存储为文本,然后对其进行评估:
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]))
Run Code Online (Sandbox Code Playgroud)
[0,1,2,3,4,5] class'list'class'int'
[0,1,2,3,4,5] class'list'class'int'
小智 8
如果需要,您可以使用 numpy 的保存功能将列表保存为文件。假设你有两个列表
sampleList1=['z','x','a','b']
sampleList2=[[1,2],[4,5]]
Run Code Online (Sandbox Code Playgroud)
这是将列表保存为文件的功能,请记住您需要保留扩展名 .npy
def saveList(myList,filename):
# the filename should mention the extension 'npy'
np.save(filename,myList)
print("Saved successfully!")
Run Code Online (Sandbox Code Playgroud)
这是将文件加载到列表中的函数
def loadList(filename):
# the filename should mention the extension 'npy'
tempNumpyArray=np.load(filename)
return tempNumpyArray.tolist()
Run Code Online (Sandbox Code Playgroud)
一个工作示例
>>> 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']
Run Code Online (Sandbox Code Playgroud)
pickle和其他序列化包工作。将它写入一个.py文件,然后您可以导入该文件也是如此。
>>> 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]
Run Code Online (Sandbox Code Playgroud)
我不喜欢很多答案是它通过写入每行文件行来进行太多系统调用。恕我直言,最好用 '\n' (行返回)加入列表,然后只将它写入文件一次:
mylist = ["abc", "def", "ghi"]
myfile = "file.txt"
with open(myfile, 'w') as f:
f.write("\n".join(mylist))
Run Code Online (Sandbox Code Playgroud)
然后打开它并再次获取您的列表:
with open(myfile, 'r') as f:
mystring = f.read()
my_list = mystring.split("\n")
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
104552 次 |
| 最近记录: |