Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python search and replace in binary file

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

After

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

the following error occurs:

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

How best to do this?

like image 264
ajo Avatar asked Jul 02 '10 00:07

ajo


People also ask

How do you update a binary file in Python?

Program Logic: Include pickle module in program using import statement. Give value of roll number from user using input() function and store it in variable say roll.

Can you find and replace in Python?

When working with strings in Python, you may need to search through strings for a pattern, or even replace parts of strings with another substring. Python has the useful string methods find() and replace() that help us perform these string processing tasks.

How does Python handle binary files?

To open a file in binary format, add 'b' to the mode parameter. Hence the "rb" mode opens the file in binary format for reading, while the "wb" mode opens the file in binary format for writing. Unlike text files, binary files are not human-readable. When opened using any text editor, the data is unrecognizable.

How do you append to a binary file in Python?

So, to append any new data to our binary file we need to use append() function same as we did in writing data to binary file in python. Just only difference is the file mode. We use “ab” for appending data to our Binary File.


2 Answers

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

like image 191
John La Rooy Avatar answered Oct 11 '22 07:10

John La Rooy


You must be using Python 3.X. You didn't define 'name' in your example, but it is the problem. Likely you defined it as a Unicode string:

name = 'blah'

It needs to be a bytes object too:

name = b'blah'

This works:

Python 3.1.2 (r312:79149, Mar 21 2010, 00:41:52) [MSC v.1500 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> f = open('file.txt','rb')
>>> s = f.read()
>>> f.close()
>>> s
b'Test File\r\n'
>>> name = b'Replacement'
>>> s=s.replace(b'File',name)
>>> s
b'Test Replacement\r\n'

In a bytes object, the arguments to replace must both be bytes objects.

like image 11
Mark Tolonen Avatar answered Oct 11 '22 06:10

Mark Tolonen