当前位置:网站首页>Python saves the list data

Python saves the list data

2020-11-06 01:27:57 Elementary school students in IT field

For a list a[]:

preservation

filename = open(‘a.txt’, ‘w’)
for value in a:
filename.write(str(value))
filename.close()

Read

f= open(“a.txt”,“r”)
a = f.read()
f.close()
Although this method takes up less space , But the original list Format will be broken . You can also use the following method for this purpose , This method can keep list Original format .

preservation

import numpy as np
a=np.array(a)
np.save(‘a.npy’,a) # Save as .npy Format

Read

a=np.load(‘a.npy’)
a=a.tolist()
ps:
If it reads .txt The document is in Chinese , You need to add the following :

readme = pd.read_csv(‘ Read me .txt’,sep=’:’,encoding=“utf-8”, engine=‘python’,header=None)
readme = np.array(readme)

 WeChat ID

版权声明
本文为[Elementary school students in IT field]所创,转载请带上原文链接,感谢