Python search and replace in binary file

前端 未结 2 1665
旧时难觅i
旧时难觅i 2021-02-12 19:19

I am trying to search and replace some of the text (eg \'Smith, John\') in this pdf form file (header.fdf, I presumed this is treated as binary file):

\'%FDF-1.2         


        
2条回答
  •  悲哀的现实
    2021-02-12 19:48

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

    or

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

    probably the latter, as I don't think you are going to be able to use unicode names with this type of substitution anyway

提交回复
热议问题