Search code examples
pythonpython-3.xmatrixexportline-breaks

Python - exporting matrix with different list lenghs


I want to export a matrix into a .txt file. My code for the export looks like this:

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)

My problem is that this creates line breaks in my .txt file where they should not be like this:

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  ]

Is there a way I can remove these unwanted linebreaks?


Solution

  • While I still don't know the origin of my problem, I found a way to remove the linebreaks. I added:

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

    to the end of my code, just before

    file.write(out_string)