python 将ASCII码的 numpy.ndarray转换为string

import numpy as np

name_ndarray = np.array([109, 97, 121, 117, 110, 0, 0, 0, 0, 0, 0, 0])  # numpy.ndarray 中的元素, 是ASCII码
name_str = ''.join(chr(x) if x else "" for x in name_ndarray)  # 遍历 numpy.ndarray 中的元素, 并将ASCII码转成字符,拼接成字符串
print(name_str)  # mayun
print(len(name_str))  # 5, 上边获取字符串的代码行,要有 if x else "" 判断, 否则,0会被转换成空字符,导致获得的字符串长度为12, 后边打印 No!

if "mayun" == name_str:
    print("Yes!")
else:
    print("No!")

你可能感兴趣的:(numpy,python)