python - 将整数数组转换为 unicode

标签 python python-3.x python-2.7

我有一个数组 arr=[07,10,11,05]。 我实际上希望将这些值转换为十六进制并将其发送到函数。我知道我可以将这些值单独转换为十六进制,例如 hex(arr[0])、hex(arr[1]) 等。我现在所做的是将数组转换为 unicode as arr =[u'07 0a 0b 05'] 手动然后发送到为我服务的函数。但是当我必须向 arr 中的 unicode 字符串发送数百个字符串(如

中所示)时,这太不切实际了
arr = [u'a3 06 06 01 00 01 01 00',
u'a3 06 06 02 00 01 02 00',
u'a3 06 06 03 00 01 03 00',
u'a3 06 06 04 00 01 04 00',
u'a3 06 06 05 00 01 05 00',
u'a3 06 06 06 00 01 06 00',
u'a3 06 06 07 00 01 07 00']

我使用循环将其一一发送(arr[i])。有没有一种方法可以动态地将整数数组转换为 unicode 并将其发送到易于接受此类 unicode 字符串的外部函数?

最佳答案

' '.join(map(hex,arr)) 是您要找的吗?

更具体地说:

import numpy as np

a = np.array([[1,2,3],[6,11,23]])

def strip_hexstring(s):
    return s.strip('0x').strip('L')

def format_line(line):
    return u'{}'.format(' '.join(map(strip_hexstring,map(hex,line))))

arr = [format_line(a[i,:]) for i in range(a.shape[0])]

如有必要,您可以像示例中那样用零填充。

关于python - 将整数数组转换为 unicode,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55737995/

相关文章:

python - 使用依赖图执行 Celery 任务

python - MAC 中的 2 个 mysql 实例

python - 我想用列表中第一次出现的字符串替换该字符串

python - 将 Python 文件夹移动到 Program Files 文件夹?

python - 如何使用 python 在 networkx 中查找不同的组?

Python - 如何使用 PySAL 计算交互式空间自相关 (Moran I)?

python - python中的变量范围和Try Catch

javascript - Python3将一堆javascript变量从网页抓取到python dict对象中

python - 如何检查Python中type()方法的返回值?

python - 如何在 python 列表中使用切片运算符避免 for 循环中的副作用?