.7文件包含来自GE扫描仪的MRS数据。我想读取它的头,更改它的一些字段值,如扫描日期等,并保存一个新的.7文件,修改头和其他数据应该是相同的。
代码:
from ctypes import * import ge_util as utilge class PfileHeaderLittle(LittleEndianStructure): """ Contains the ctypes Structure for a GE P-file rdb header. Dynamically allocate the ctypes _fields_ list later depending on revision """ _pack_ = 1 _fields_ = utilge.get_pfile_hdr_fields(20.0) hdr = PfileHeaderLittle() fname = r"P16896.7" filelike = open(fname, 'rb') filelike.seek(0) filelike.readinto(hdr) print("hdr: ", hdr) print("hdr: ", type(hdr.rhe_patname)) print("hdr: ", hdr.rhe_patname) hdr.rhe_patname = b"CHANGE_IT" print("hdr: ", hdr.rhe_patname) with open("my_file.7", 'wb') as file: file.write(hdr)
输出:
hdr: <__main__.PfileHeaderLittle object at 0x0000025F75D09F48> hdr: <class 'bytes'> hdr: b'MRS TEST1' hdr: b'CHANGE_IT'
此代码读取标题,修改一个字段并将其保存回。但是,其余的数据不会被保存。我再次读取保存的文件,并确认头已修改,但新文件中不存在其余数据。 如何保存修改后的头文件并用其余数据保存新文件?
这是 ge_util : https://scion.duhs.duke.edu/vespa/project/export/3828/trunk/common/ge_util.py P16896.7 文件: https://drive.google.com/open?id=1zqQbOUwlIa1_rv9OAVA0sQfEEaOFsne0
ge_util
P16896.7
一旦你修改了 hdr ,查找其字节长度 len(bytes(hdr)) . 使用 seek 去那个地方然后 read() 文件的其余部分。现在,将其与新的头连接起来,并保存一个新文件:
hdr
len(bytes(hdr))
seek
read()
filelike.seek(len(bytes(hdr))) b = filelike.read() c = bytes(hdr)+b file = open('New_File.7', 'wb') file.write(c) file.close()