如何读取、修改 header 并保存 .7 文件及其其余数据?
How to read, modify header and save .7 file with rest of its data?
.7 文件包含来自 GE 扫描仪的 MRS 数据。我想读取它的 header,更改它的一些字段值,如扫描日期等,并保存一个修改过 header 的新 .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'
此代码读取header,修改一个字段并保存回来。但是,其余数据未保存。我再次阅读保存的文件并确认 header 已修改,但新文件中不存在其余数据。 如何保存修改后的 header 并保存包含其余数据的新文件?
这是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
一旦你修改了hdr
,通过len(bytes(hdr))
找到它的字节长度。使用 seek
到达该位置并使用 read()
文件的其余部分。现在,将其与新的 header 连接起来,并保存一个新文件:
filelike.seek(len(bytes(hdr)))
b = filelike.read()
c = bytes(hdr)+b
file = open('New_File.7', 'wb')
file.write(c)
file.close()
.7 文件包含来自 GE 扫描仪的 MRS 数据。我想读取它的 header,更改它的一些字段值,如扫描日期等,并保存一个修改过 header 的新 .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'
此代码读取header,修改一个字段并保存回来。但是,其余数据未保存。我再次阅读保存的文件并确认 header 已修改,但新文件中不存在其余数据。 如何保存修改后的 header 并保存包含其余数据的新文件?
这是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
一旦你修改了hdr
,通过len(bytes(hdr))
找到它的字节长度。使用 seek
到达该位置并使用 read()
文件的其余部分。现在,将其与新的 header 连接起来,并保存一个新文件:
filelike.seek(len(bytes(hdr)))
b = filelike.read()
c = bytes(hdr)+b
file = open('New_File.7', 'wb')
file.write(c)
file.close()