【问题标题】:numpy slicing - TypeError: only integer scalar arrays can be converted to a scalar indexnumpy 切片 - 类型错误:只有整数标量数组可以转换为标量索引
【发布时间】:2019-11-07 12:12:34
【问题描述】:

数据文件:pattern1.ktx

import numpy as np

data = np.fromfile('pattern1.ktx', dtype=np.byte)

print ('endianness:', hex(data[12:13]))

结果: TypeError: 只有整数标量数组可以转换为标量索引

看起来很简单?我不明白。如何解决这个问题?谢谢。

这个怎么样?

    b = np.frombuffer(np.array(data[12:12+4], dtype=np.byte), dtype=np.uint32)
    print ('endianess:', hex(b))

同样的错误。如何解决?

【问题讨论】:

  • hex(data[12])
  • 是的,就是这样,大脑冻结。刚刚伸手去拿咖啡。谢谢。

标签: python python-3.x numpy


【解决方案1】:

好的,由于某种原因它返回了一个列表,所以必须对其进行索引。

b = np.frombuffer(np.array(data[12:12+4], dtype=np.byte), dtype=np.uint32)
print ('endianess:', hex(b[0]))

错误已修复。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-07-15
    • 1970-01-01
    • 2021-10-31
    • 2018-05-29
    • 2018-04-04
    • 2021-07-21
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多