加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Python > 正文

Python在二进制文件中搜索和替换

发布时间:2020-12-16 23:09:40 所属栏目:Python 来源:网络整理
导读:我试图在这个pdf格式文件(header.fdf,我推测这被视为二进制文件)中搜索和替换一些文本(例如“Smith,John”): '%FDF-1.2n%xe2xe3xcfxd3n1 0 objn/FDF/Fields[/V(M)/T(PatientSexLabel)/V(24-09-1956 53)/T(PatientDateOfBirth)/V(Fisher)/T(PatientLa
我试图在这个pdf格式文件(header.fdf,我推测这被视为二进制文件)中搜索和替换一些文本(例如“Smith,John”):
'%FDF-1.2n%xe2xe3xcfxd3n1 0 objn<</FDF<</Fields[<</V(M)/T(PatientSexLabel)>><</V(24-09-1956  53)/T(PatientDateOfBirth)>><</V(Fisher)/T(PatientLastNameLabel)>><</V(CNSL)/T(PatientConsultant)>><</V(28-01-2010 18:13)/T(PatientAdmission)>><</V(134 Field StreetrBlackburn BB1 1BB)/T(PatientAddressLabel)>><</V(Smith,John)/T(PatientName)>><</V(24-09-1956)/T(PatientDobLabel)>><</V(0123456)/T(PatientRxr)>><</V(01234567891011)/T(PatientNhsLabel)>><</V(John)/T(PatientFirstNameLabel)>><</V(0123456)/T(PatientRxrLabel)>>]>>>>nendobjntrailern<</Root 1 0 R>>n%%EOFn'

f=open("header.fdf","rb")
s=f.read()
f.close()
s=s.replace(b'PatientName',name)

发生以下错误:

Traceback (most recent call last):
  File "/home/aj/Inkscape/Med/GAD/gad.py",line 56,in <module>
    s=s.replace(b'PatientName',name)
TypeError: expected an object with the buffer interface

怎么最好这样做?

解决方法

f=open("header.fdf","rb")
s=str(f.read())
f.close()
s=s.replace(b'PatientName',name)

要么

f=open("header.fdf",bytes(name))

可能是后者,因为我不认为你将能够使用unicode名称与这种类型的替代

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读