How to convert numpy object array into str/unicode array?

后端 未结 2 1840
执笔经年
执笔经年 2021-02-07 13:15

Update: In lastest version of numpy (e.g., v1.8.1), this is no longer a issue. All the methods mentioned here now work as excepted.

Original qu

相关标签:
2条回答
  • 2021-02-07 13:47

    At least in Python 3.5 Jupyter 4 I can use:

    a=np.array([u'12345',u'abc'],dtype=object)
    b=a.astype(str)
    b
    

    works just fine for me and returns:

    array(['12345', 'abc'],dtype='<U5')
    
    0 讨论(0)
  • 2021-02-07 13:55

    I know this is an old question but in case anyone comes across it and is looking for an answer, try

    c = a.astype('U')
    

    and you should get the result you expect:

    c = array([u'abc', u'12345'], dtype='<U5')
    
    0 讨论(0)
提交回复
热议问题