python输出字符串中的元素_漂亮地编写和打印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' '12345 ABCDEFG' '1A 2B3C' '\nE N D']

Is there a way I can write the contents of arr to a file without the [, ] and without the ' for each numpy.str_ element?

That is, to write arr like this:

\tSTART\t 0\n12345 ABCDEFG1A 2B3\nE N D

Also, can I print the elements of the numpy array one element per line? Ideally, here as well without the [, ] and without the '.

That is, to print arr like this:

\tSTART\t 0\n

12345 ABCDEFG

1A 2B3

\nE N D

EDIT

(1) If try this

with open(resultfile, 'w') as f:

f.write(str(arr))

I get

['\tSTART\t 0\n' '12345 ABCDEFG' '1A 2B3C' '\nE N D']

(2) If I try this (as suggested by GreenHawk1220's answer)

A = str('\n'.join(arr))

B = ''.split(A)

del B[0]

# del B[-1] #Deletes end characters # *THIS GIVES INDEXERROR*

C = ''.join(B)

print(C)

with open(resultfile, 'w') as f:

f.write(C)

I get nothing (and nothing is writtent to the file).

解决方案

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='

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 0

12345 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 0

12345 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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值