Python - 导出具有不同列表长度的矩阵

时间:2017-02-18 21:12:56

标签: python python-3.x matrix export line-breaks

我想将矩阵导出到.txt文件中。我的导出代码如下所示:

file = open("C:/Users/User/Desktop/Output.txt", "w")
file.write("Matrix1" + "\n")
for i in range(len(mat1)):
    out_string = ""
    out_string += str(mat1[i])
    file.write(out_string)

我的问题是,这会在我的.txt文件中创建换行符,它们不应该是这样的:

Matrix1
[-0.46957269  0.17011335 -0.47471708  0.57137096 -0.79282618  0.30592412][-0.63786411 -0.73155594 -0.42771667  0.86549777  0.88929707  0.58907694][  878.71032715  1400.89709473  1277.76208496  1393.41540527  1170.13269043
844.46856689][  878.83557129  1399.39916992  1279.01879883  1394.15820312 1169.20703125
844.88671875][ 0.98017752  0.88977867  0.38268465 -0.91065341 -0.32004485 0.53876978][-0.31741497 -0.70884681  0.01819103  0.97179461 -0.75332266 -0.247715  ]

有没有办法可以删除这些不需要的换行符?

1 个答案:

答案 0 :(得分:0)

虽然我仍然不知道问题的根源,但我找到了一种方法来删除换行符。我补充说:

out_string = out_string.replace("\n","")

到我的代码的末尾,就在

之前
file.write(out_string)