【问题标题】:Binary file to python integer list二进制文件到python整数列表
【发布时间】:2014-06-10 06:04:45
【问题描述】:

我有一个存储为二进制文件的 1,000,000 个整数的列表。如何快速将其加载到 Python 列表中?在 C 中,我只需将文件读入 char 数组并将该数组转换为 int 数组。有没有办法在 Python 中做与此等效的事情?我知道 Python 的 struct 模块,但据我所知,这需要一个非常长的格式字符串来一次转换所有整数。

【问题讨论】:

  • struct.unpack('1000000I',f.read()) 对我来说似乎不太长。
  • 如果你不喜欢struct,使用r=array.array('i');r.fromfile(...)
  • 谢谢@roippi。我不知道您可以在格式字符串中指定重复。应该更仔细地阅读文档:)

标签: python c arrays python-2.7 io


【解决方案1】:

struct.unpack('1000000I',f.read()) 对我来说似乎不太长。 – roippi

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-10-09
    • 1970-01-01
    • 2015-01-30
    • 2016-08-22
    • 2011-01-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多