Home > Back-end >  Python: write to file results in truncated output ([ 1. 1.03 1.07... 79.70) ] instead of column of a
Python: write to file results in truncated output ([ 1. 1.03 1.07... 79.70) ] instead of column of a

Time:05-12

I'm not that experienced in Python.

I want to write a variable called "abc" to a file. It should be a long column of numbers, like:

1.000
1.002
2.7778
...

I can create a file and write output to it. However, no matter what I try, I get something like the below, where there's an ellipsis (...) instead of all of the numbers:

abc: [ 1.          1.03407826  1.07976092 ... 79.70580163 79.70587576
 79.7059467 ]

I have spent a few hours searching on this problem and all I can find is suggestions to flush the stream by using:

file.close()

That makes no difference for me.

Here are the various things I have tried (below). I thought the top one would work. When it didn't, I started searching for other methods. So I don't really understand how the following methods should work.

with open("abc.txt", "w") as text_file:
    text_file.write("ABC: %s" % abc)

text_file = open("abc.txt", "w")
text_file.write("ABC: %s" % abc)
text_file.close()

with open("abc.txt", "w") as text_file:
    text_file.write("ABC: {0}".format(abc))
    
with open("abc.txt", "w") as text_file:
    print("ABC: {}".format(abc), file=text_file)

with open("abc.txt", "w") as text_file:
    print(f"ABC: {abc}", file=text_file)

Because all of the methods lead to the same problem, I think there must be something more fundamental I'm getting wrong. If anyone could please point me in the right direction, I would appreciate it.


Update:

I tried @JohnnyMopp's suggestion, which, if I understand correctly, should be:

text_file = open("abc.txt", "w")
for line in abc: text_file.write(f"{line}\n"
text_file.close()

I had a problem: f.write(str('\n'.join(abc))) TypeError: sequence item 0: expected str instance, numpy.float64 found

It looks like abc is in numpy.float64 form so I did this (a suggestion I saw earlier in another post):

text_file = open("abc.txt", "w")
for line in str(abc): text_file.write(f"{line}\n"
text_file.close()

I got:

  File "abc.py", line 176
    text_file.close()
    ^
SyntaxError: invalid syntax

CodePudding user response:

Frankly I don't understand where is the problem.

You should simply use for-loop to display every value in separated line

import numpy as np

abc = np.array([1., 1.03407826, 1.07976092, 79.70580163, 79.70587576, 79.7059467])

print('--- row --- ')

print(abc)

print('--- column --- ')

for value in abc:
    print(value)

Result:

--- row --- 
[ 1.          1.03407826  1.07976092 79.70580163 79.70587576 79.7059467 ]
--- column --- 
1.0
1.03407826
1.07976092
79.70580163
79.70587576
79.7059467

And the same way you can write in file

with open('output.txt', 'w') as f:

    for value in abc:
        print(value, file=f)

or

with open('output.txt', 'w') as f:

    for value in abc:
        f.write(f"{value}\n")

and if you want to use str() then you have to use it on every value separatelly

with open('output.txt', 'w') as f:

    for value in abc:
        f.write( str(value)   "\n")

and if you want to use join() then you also have to use str() on every value separatelly

with open('output.txt', 'w') as f:

    text = "\n".join( str(value) for value in abc )
    f.write( text )

or

with open('output.txt', 'w') as f:

    text = "\n".join( map(str, abc) )
    f.write( text )

Eventually you could use numpy to save it

np.savetxt('output2.txt', data, fmt='%f')    

With fmt=%f it saves with (default) 6 decimal digits
(but you can use ie. %.2f to save only 2 decimal digits)

1.000000
1.034078
1.079761
79.705802
79.705876
79.705947

Without fmt it saves as

1.000000000000000000e 00
1.034078260000000027e 00
1.079760920000000013e 00
7.970580162999999629e 01
7.970587575999999785e 01
7.970594669999999837e 01

EDIT:

Full working code with all examples - so everyone can simply copy and run it.

import numpy as np


abc = np.array([1., 1.03407826, 1.07976092, 79.70580163, 79.70587576, 79.7059467])

print('--- row --- ')

print(abc)

print('--- column --- ')

for value in abc:
    print(value)
    
# ---------------------------

with open('output-1.txt', 'w') as f:

    for value in abc:
        print(value, file=f)

# ---------------------------
    
with open('output-2.txt', 'w') as f:

    for value in abc:
        f.write(f"{value}\n")
        
# ---------------------------
    
with open('output-3.txt', 'w') as f:

    for value in abc:
        f.write( str(value)   "\n")
        
# ---------------------------
    
with open('output-4.txt', 'w') as f:

    for value in abc:
        f.write( str(value)   "\n")

# ---------------------------
    
with open('output-5.txt', 'w') as f:

    text = "\n".join(str(value) for value in abc)
    f.write( text )

with open('output-6.txt', 'w') as f:

    text = "\n".join( map(str, abc) )
    f.write( text )
    
# ---------------------------
        
np.savetxt('output-7.txt', abc)

# ---------------------------

np.savetxt('output-8.txt', abc, fmt='%f')

# ---------------------------

np.savetxt('output-9.txt', abc, fmt='%.2f')
  • Related