打开文件
f = open('C:\\Users\\Tesla\\Desktop\\pjxinjiang30')
reader = list(csv.reader(f))
arr = np.array(reader)
date = arr.T
定义空矩阵
mat = np.zeros(shape=(0,5))
范围内按条件查找
for i in range(20200716,20200804):
condition = (date == ' ' + str(i) + ' ')
buffer = list(np.where(condition))
if len(buffer[0]) != 0:
buffer = arr[buffer[0][0] : buffer[0][len(buffer[0]) - 1] + 1]
buffer = np.random.permutation(buffer)
result_mat = np.r_[result_mat,buffer]
新建index
result_mat = np.delete(result_mat,0,axis = 1)
index = (np.arange(0,len(reader), 1)).reshape(len(reader),1)
result_mat = np.c_[index,result_mat]
将矩阵写入文件
file = open('C:\\Users\\Tesla\\Desktop\\data','w')
file.write(str(result_mat));
file.close()
解决numpy写入文件出现省略号
np.set_printoptions(threshold = np.inf)
numpy写入文件宽度(自动换行)
np.set_printoptions(linewidth=1000)