For a list a[]:
filename = open(‘a.txt’, ‘w’)
for value in a:
filename.write(str(value))
filename.close()
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 .
import numpy as np
a=np.array(a)
np.save(‘a.npy’,a) # Save as .npy Format
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)