python读取txt二维数组,做转置后二维数组输出到txt
import numpy as np
def read_checkpoints():
checkpoints_from_file = []
with open("new2.txt", "r") as f:
lines = f.readlines()
for line in lines:
line = line.replace("n", "")
checkpoints_from_file.append(line.split("t"))
#print(checkpoints_from_file)
checkpoints_from_file = np.array(checkpoints_from_file).transpose()
print(len(checkpoints_from_file))
print(len(checkpoints_from_file[0]))
print(checkpoints_from_file[0])
print(checkpoints_from_file[1])
print(checkpoints_from_file[41])
return checkpoints_from_file
def Writedata(data):
filename = 'specSamps_selectedArea.txt' #数据文件保存位置
row = np.array(data).shape[0] #获取行数n
col = np.array(data).shape[1] #获取行数n
with open(filename,'w') as f: # 若filename不存在会自动创建,写之前会清空文件
for rowIdx in range(0,row):
jointsFrame = data[rowIdx] #每行
jointText = ' '.join(jointsFrame)
f.write(jointText)
#for colIdx in range(0,col):
# strNum = str(jointsFrame[colIdx])
# f.write(strNum)
f.write('n')
f.close()
if __name__ == '__main__':
np.set_printoptions(threshold=np.inf)
data = read_checkpoints()
Writedata(data)