Skip to content Skip to sidebar Skip to footer

Pretty Write And Print String Elements Of A Numpy Ndarray (python)

Say we have an numpy.ndarray with numpy.str_ elements. For example, below arr is the numpy.ndarray with four numpy.str_ elements like this: >>> print(arr) ['\tSTART\t 0\n

Solution 1:

Make the array:

In [2]: arr = np.array(['\tSTART\t  0\n',  '12345 ABCDEFG',  '1A 2B3C',  '\nE N D'])
In [3]: 
In [3]: arr
Out[3]: 
array(['\tSTART\t  0\n', '12345 ABCDEFG', '1A 2B3C', '\nE N D'], 
      dtype='<U13')

Join into one string; equivalent of ''.join(arr.tolist()) (actually ''.join(list(arr)))

In [4]: ''.join(arr)
Out[4]: '\tSTART\t  0\n12345 ABCDEFG1A 2B3C\nE N D'

The print/str representation expands on the \n and \t.

In [5]: print(''.join(arr))
    START     012345 ABCDEFG1A 2B3C
E N D

the repr quotes them:

In [6]: print(repr(''.join(arr)))
'\tSTART\t  0\n12345 ABCDEFG1A 2B3C\nE N D'

f.write has the same issues:

In [8]: with open('test.txt','w') as f:
   ...:     f.write(''.join(arr))
   ...:     f.write('\n')
   ...:     
In [9]: cat test.txt
    START     012345 ABCDEFG1A 2B3C
E N D
In [10]: with open('test.txt','w') as f:
    ...:     f.write(repr(''.join(arr)))
    ...:     f.write('\n')
    ...:     
In [11]: cat test.txt
'\tSTART\t  0\n12345 ABCDEFG1A 2B3C\nE N D'

This isn't really a string array issue. It's a question of how to print/write a string that contains \n and \t.

Following the comment:

In [18]: with open('test.txt','wb') as f:
    ...:     f.write(''.join(arr).encode('unicode_escape'))
    ...:     f.write(b'\n')
In [19]: cat test.txt
\tSTART\t  0\n12345 ABCDEFG1A 2B3C\nE N D

and for the individual strings:

In [21]: with open('test.txt','wb') as f:
    ...:     for s in arr:
    ...:         f.write(s.encode('unicode_escape'))
    ...:         f.write(b'\n')
    ...:         
In [22]: cat test.txt
\tSTART\t  0\n
12345 ABCDEFG
1A 2B3C
\nE N D

Just in case it isn't obvious, I'm using Ipython with Py3. Py2 might be different.

The encode creates a bytestring with extra \\t etc. .decode can be used to turn it back into unicode for neat printing:

In [6]: for s in arr: print(s.encode('unicode_escape').decode())
\tSTART\t  0\n
12345 ABCDEFG
1A 2B3C
\nE N D

Post a Comment for "Pretty Write And Print String Elements Of A Numpy Ndarray (python)"