值错误:将二进制文件中的数据存储到numpy 3d数组中

2024-05-29 02:00:13 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图用Struct模块从二进制文件中读取浮点数,然后将它们存储在numpy3d中数组。什么时候我把它作为一个独立的脚本运行,它能工作好吧。但是当我从另一个脚本调用它作为类的函数时(在导入之后),它会给出值错误 这是我的密码。在

import struct
from numpy import *
class DCD_read:
   def read_cord(self,total_atoms,dcd_data):
        cord_data=dcd_data[276:len(dcd_data)]        ##  binary data string
        byte=0
        count=0
       total_frames=info_dict['nset']
       coord=numpy.zeros((total_frames,total_atoms,3))         ## 3d array
       for frames in range(0,total_frames):
           for atoms in range(0,total_atoms):
              x = list(struct.unpack('<f',cord_data[60+byte:64+byte])) ###reading float
              byte+=4
              y = list(struct.unpack('<f',cord_data[60+byte:64+byte]))
              byte+=4
              z = list(struct.unpack('<f',cord_data[60+byte:64+byte]))
              byte+=4
              ls=x
              ls.extend(y)
              ls.extend(z)
              coord[frames][atoms]=ls
     return coord

错误:

^{pr2}$

在医学博士是主(调用脚本),而DCD_阅读.py是模块。这是医学博士(主脚本)

from DCD_read import *
import numpy
dcd_file=open('frame3.dcd',"rb")
dcd_data=dcd_file.read()
dcd=read_dcd()
total_atoms=6141
coord=dcd.read_cord(total_atoms,dcd_data)

有谁能帮忙吗????我希望我能解释清楚很明显。谢谢在


Tags: importnumpy脚本readdataframesbytels

热门问题