- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有一个读取二进制文件的 IDL 过程,并尝试将其转换为 Python 例程。IDL 代码如下所示:
a = uint(0)
b = float(0)
c = float(0)
d = float(0)
e = float(0)
x=dblarr(nptx)
y=dblarr(npty)
z=dblarr(nptz)
openr,11,name_file_data,/f77_unformatted
readu,11,a
readu,11,b,c,d,e
readu,11,x
readu,11,y
readu,11,z
它工作得很好。所以我在 python 中写了同样的东西,但我找不到相同的结果(甚至“a”的值不同)。这是我的代码:
x=np.zeros(nptx,float)
y=np.zeros(npty,float)
z=np.zeros(nptz,float)
with open(name_file_data, "rb") as fb:
a, = struct.unpack("I", fb.read(4))
b,c,d,e = struct.unpack("ffff", fb.read(16))
x[:] = struct.unpack(str(nptx)+"d", fb.read(nptx*8))[:]
y[:] = struct.unpack(str(npty)+"d", fb.read(npty*8))[:]
z[:] = struct.unpack(str(nptz)+"d", fb.read(nptz*8))[:]
希望对大家的回答有所帮助。
更新:正如答案中所建议的,我现在正在尝试模块“FortranFile”,但我不确定我是否理解有关其使用的所有内容。
from scipy.io import FortranFile
f=FortranFile(name_file_data, 'r')
a=f.read_record('H')
b=f.read_record('f','f','f','f')
但是,我得到的不是“a”的整数,而是:array([0, 0], dtype=uint16)。
对于“b”,我遇到以下错误:获得的大小 (1107201884) 不是给定数据类型 (16) 的倍数
最佳答案
根据a table of IDL data types ,UINT(0)
创建一个 16 位整数(即两个字节)。在 Python struct
module ,I
format character表示 4 字节整数,H
表示无符号 16 位整数。
尝试将解压 a
的行更改为
a, = struct.unpack("H", fb.read(2))
不幸的是,这可能无法解决问题。您可以将选项 /f77_unformatted
与 openr
一起使用,这意味着该文件不仅仅包含变量的原始字节。 (有关 /f77_unformatted
的更多信息,请参阅 documentation of the OPENR command。)
您可以尝试使用scipy.io.FortranFile
读取该文件,但不能保证它会起作用。未格式化的 Fortran 文件的二进制布局取决于编译器。
关于python - Python 中使用 unpack 读取二进制文件与 IDL 方法的比较,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52386182/
dependencies:unpack-dependencies 可以解压哪些存档格式?例如,据我所知,它无法解压 RPM 包。是否有它支持的格式列表?有什么技巧可以让 maven 解压 RPM 包吗
谁能给我解释一下为什么在 test1 上,nums 是 [[0,0],[-1,-1],[0,0],[0,0]] 而不是在测试2上?据我了解,python for xx in xxx 与任何其他语言中的
我想按照建议在 php 中读取一个二进制文件作为字节 [] here我 unpack 'ed fread 的输出。所以我有类似的东西: $file=fopen($filename,'r'); fsee
首先,很抱歉是否已经提出并回答了这个问题,但我认为我的情况与我一直试图找到的其他答案有所不同。另外,我对C++还是很陌生。 我基本上是想创建一个系统,其中C++程序从游戏中接收数据并将其通过套接字发送
克隆 git-repo 后,当我尝试在提交后将 repo 推送到 git 服务器时。它给出的错误是“在远程端解包过程中发生 ssh 错误:解包对象异常退出” 最佳答案 检查远程仓库的所有权。 我也遇到
解决的问题 需要将数组(list)或元组(tuple)中的元素导出到N个变量中。 解决的方案 任何序列都可以通过简单的变量赋值方式将其元素分配到对应的变量中,唯一的要求就是变量的数量和结构需要
Dim rs, ws, fso, conn, stream, connStr, theFolder
我有这个测试函数,它只是打印传递给它的值 function test1(...) for k, v in ipairs(arg) do print(v) end end func
我有以下宏: #define HEX 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00 #define BITS 0x01 #define ADD_FLAGS(a, b,
我正在阅读一个套接字,在所有问题都以为我们发送了错误的信息之后,事实证明阅读存在问题。 这个想法是: 1a- Get 4 bytes, unpack (95). 2a- Get 95 next byt
我有一个返回 Sympy 点的函数: result = [Point3D(500, 500, 10), Point3D(-500, 500, 10), Point3D(-500, -500, 10),
我正在尝试存储在 std::tuple 中不同数量的值,稍后将用作调用与存储类型匹配的函数指针的参数。 我创建了一个简化示例来展示我正在努力解决的问题: #include #include voi
我正在使用 ben strasser C++ 快速 csv 解析器:https://github.com/ben-strasser/fast-cpp-csv-parser .它使用可变参数模板将列值传
我有一个由 3 个字符串组成的打包字符串,这样我就有一个整数,指定下一个项目的字节长度,然后是那个项目的字节,然后是下一个项目的字节大小,等等,就好像有人做了: [a.bytesize, a, b.b
我正在尝试存储在 std::tuple 中不同数量的值,稍后将用作调用与存储类型匹配的函数指针的参数。 我创建了一个简化的示例来展示我正在努力解决的问题: #include #include vo
我正在尝试按照 https://conda.github.io/conda-pack/index.html?highlight=conda%20unpack 中的步骤进行操作打包和解包 conda 环
我是R的新手,可以轻松地重新组织数据,并四处寻找解决方案,但找不到我想要做的事情。 Reshape2的融化/浇铸似乎不太奏效,我还没有很好地掌握plyr,因此无法将其纳入其中。 基本上,我有一个dat
希望标题不要误导。 我像往常一样在 Pandas 数据框中加载一个 Excel 文件 df = pd.read_excel('complete.xlsx') 这就是里面的东西(通常已经订购了 - 这是
这个问题在这里已经有了答案: Lua unpack() messing arguments (1 个回答) 6年前关闭。 有人可以向我解释为什么table.unpack()仅当在 table.unpa
我有一个读取二进制文件然后使用 struct.unpack() 解压文件内容的函数。我的功能工作得很好。如果/当我使用长的“格式”字符串解压缩整个文件时,它会更快。问题是有时字节对齐会发生变化,因此我
我是一名优秀的程序员,十分优秀!