I would like to create an output in `.txt format in python. The problem is that the results are showing horizontally.
How can I set my values vertically?
for example my output has the format:
JavaScript
x
13
13
1
[6.48421466 4.28001787 6.76134729 5.45509747 7.68957008 3.25036695
2
5.26088052 4.45128821 3.99247354 4.04626299 3.19329872 5.21477152
3
5.75235725 5.4032342 4.75781543 4.86203242 4.94567754 6.2735008
4
3.90191443 3.34211125 4.80198239 5.39782033 4.65575587 4.09630464
5
4.68439523 4.24076152 2.70145788 3.18283344 2.67654271 6.71627663
6
3.99750959 4.55231039 6.57358438 4.59699555 3.37902555 4.60574622
7
5.7602282 5.34084772 4.2033163 4.41813674 5.83988272 4.56814295
8
4.22884378 3.75609531 4.54537646 4.82880385 4.4317394 4.69930332
9
5.46046878 3.38346653 4.02209524 4.73886735 4.91038119 3.83070474
10
3.46198489 4.89056201 3.45052842 3.60843658 5.38378215 5.82383583
11
3.37329096 3.72459568 5.42039616 5.4329635 5.16597499 3.61643261
12
5.51898447 4.75482025 4.43989681 4.71631944 5.04887236 4.16837725]
13
I have tried:
JavaScript
1
3
1
with open('outputdata.txt','w',encoding='utf-8') as fout:
2
fout.writelines(str(A))
3
and
JavaScript
1
3
1
with open('outputdata.txt','w',encoding='utf-8') as fout:
2
fout.write(str(A)+'n')
3
But it is no use. My results are showing horizontally, and not vertically (with a column format I mean)
Advertisement
Answer
Writing to a file will always be horizontally unless you manually write each value in a different line. I suggest you use this simple foreach loop:
JavaScript
1
4
1
with open('outputdata.txt','w') as fout:
2
foreach item in A:
3
fout.write(str(item) + "n")
4
Of course, you can add “[” at the beginning and “]” at the end if necessary. Good Luck!