Python利用Numpy读取文本文件,存储文本文件

# Author: Baozi
#-*- codeing:utf-8 -*-
'''
数据文件:ttt.txt内容:(以空格分开每个数据)
0.26 1.00 0.41
0.57 0.40 0.14
0.04 0.72 0.45
'''
import numpy,sys
def matrix_transpose(inpath,outpath):
    matrix = numpy.loadtxt(inpath)
    print("原矩阵:")#输出原矩阵
    print(matrix)#输出原矩阵
    matrix_trans = matrix.transpose()
    print("转置后矩阵:")#输出转置后的矩阵
    print(matrix_trans)#输出转置后的矩阵
    numpy.savetxt(outpath,matrix_trans,fmt="%.2f %.2f %.2f")

if __name__ == '__main__':
    inpath= str(sys.argv[1])
    outpath= str(sys.argv[2])
    # inpath = input("输入路径:")
    # outpath = input("输出路径:")
    matrix_transpose(inpath,outpath)

你可能感兴趣的:(python学习记录笔记)