-3

I want to write the output of my phyton program in a text file. The output is in a loop and I want that each output be in one line. I am using this code for writing, but still I will give all the result in one line.

with open('temp.txt', 'w') as outfile:
    out = P2[16:3276,:].sum(axis=0)
    outfile.write("{}\n".format(out))

The actual result is:

[ 0.  0.  0. ...,  0.  0.  0.]

But I want to be like this:

0
0
0
0
0
0
Zahra
  • 43
  • 8

2 Answers2

1

Use str.join instead:

with open('temp.txt', 'w') as outfile:
    out = P2[16:3276,:].sum(axis=0)
    outfile.write("\n".join(map(lambda x: '%f' % x, out)))
U13-Forward
  • 69,221
  • 14
  • 89
  • 114
0

u can use replace, depend what's your output, but it can work

r = { "[": "", ".": "\n", "]": ""}

for x,y in r.items():
        out = out.replace(x, y)
Octaveojk
  • 11
  • 6