python处理二进制文件_Python在二进制文件中搜索和替换

我试图在这个pdf格式文件(header.fdf,我推测这被视为二进制文件)中搜索和替换一些文本(例如“Smith,John”):

'%FDF-1.2\n%\xe2\xe3\xcf\xd3\n1 0 obj\n<<[<(M)/T(PatientSexLabel)>><(24-09-1956 53)/T(PatientDateOfBirth)>><(Fisher)/T(PatientLastNameLabel)>><(CNSL)/T(PatientConsultant)>><(28-01-2010 18:13)/T(PatientAdmission)>><(134 Field Street\\rBlackburn BB1 1BB)/T(PatientAddressLabel)>><(Smith,John)/T(PatientName)>><(24-09-1956)/T(PatientDobLabel)>><(0123456)/T(PatientRxr)>><(01234567891011)/T(PatientNhsLabel)>><(John)/T(PatientFirstNameLabel)>><(0123456)/T(PatientRxrLabel)>>]>>>>\nendobj\ntrailer\n<1 0 R>>\n%%EOF\n'

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

s=s.replace(b'PatientName',name)

TypeError: expected an object with the buffer interface

怎么最好这样做?

你可能感兴趣的:(python处理二进制文件)