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"))
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]
col = np.array(data).shape[1]
with open(filename,'w') as f:
for rowIdx in range(0,row):
jointsFrame = data[rowIdx]
jointText = ' '.join(jointsFrame)
f.write(jointText)
f.write('\n')
f.close()
if __name__ == '__main__':
np.set_printoptions(threshold=np.inf)
data = read_checkpoints()
Writedata(data)