我正在尝试搜索并替换此pdf格式文件(header.fdf,我假定它被视为二进制文件)中的一些文本(例如“Smith,John”):
'%FDF-1.2\n%\xe2\xe3\xcf\xd3\n1 0 obj\n<</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 Street\\rBlackburn 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)>>]>>>>\nendobj\ntrailer\n<</Root 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 <module>
s=s.replace(b'PatientName',name)
TypeError: expected an object with the buffer interface
如何才能做到最好?
您一定在使用Python3.X。您没有在示例中定义“name”,但这是问题所在。可能您将其定义为Unicode字符串:
它也需要是一个bytes对象:
这是有效的:
在
bytes
对象中,要替换的参数必须同时是bytes
对象。或者
可能是后者,因为我认为您无论如何都不能在这种类型的替换中使用unicode名称
相关问题 更多 >
编程相关推荐