简体   繁体   中英

Convert Numpy array of ASCII codes to string

I would like to convert a NumPy array of integers representing ASCII codes to the corresponding string. For example ASCII code 97 is equal to character "a" . I tried:

from numpy import *
a=array([97, 98, 99])
c = a.astype('string')
print c

which gives:

['9' '9' '9']

but I would like to get the string "abc" .

print "".join([chr(item) for item in a])

输出

abc

Another solution that does not involve leaving the NumPy world is to view the data as strings:

arr = np.array([97, 98, 99], dtype=np.uint8).view('S3').squeeze()

or if your numpy array is not 8-bit integers:

arr = np.array([97, 98, 99]).astype(np.uint8).view('S3').squeeze()

In these cases however you do have to append the right length to the data type (eg 'S3' for 3 character strings).

create an array of bytes and decode the the byte representation using the ascii codec:

np.array([98,97,99], dtype=np.int8).tostring().decode("ascii")

note that tostring is badly named, it actually returns bytes which happens to be a string in python2, in python3 you will get the bytes type back which need to be decoded.

from numpy import array

a = array([97, 98, 99])
print("{0:c}{1:c}{2:c}".format(a[0], a[1], a[2]))

Of course, join and a list comprehension can be used here as well.

Solutions that rely on Python loops or string formatting will be slow for large datasets. If you know that all of your data are ASCII, a faster approach could be to use fancy indexing:

import numpy as np
a = np.array([97, 98, 99])
np.array([chr(x) for x in range(127)])[a]
# array(['a', 'b', 'c'], dtype='<U1')

An advantage is that it works for arbitrarily shaped arrays.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM