您好, 欢迎来到 !    登录 | 注册 | | 设为首页 | 收藏本站

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

5b51 2022/1/14 8:22:28 python 字数 1862 阅读 560 来源 www.jb51.cc/python

我试图在这个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(Fi

概述

'%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

怎么最好这样做?

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

要么

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

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

总结

以上是编程之家为你收集整理的Python在二进制文件中搜索和替换全部内容,希望文章能够帮你解决Python在二进制文件中搜索和替换所遇到的程序开发问题。


如果您也喜欢它,动动您的小指点个赞吧

除非注明,文章均由 laddyq.com 整理发布,欢迎转载。

转载请注明:
链接:http://laddyq.com
来源:laddyq.com
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。


联系我
置顶