I am undertaking conversion of my python application from python 2 to python 3. One of the functions which I use is to get the printable character out of binary file. I earlier used following function in python 2 and it worked great:
import string
def strings(filename, min=4):
with open(filename, "rb") as f:
result = ""
for c in f.read():
if c in string.printable:
result += c
continue
if len(result) >= min:
yield result
result = ""
if len(result) >= min: # catch result at EOF
yield result
Code is actually from Python equivalent of unix "strings" utility. When I run the above code with python 2 it produces the output like this which is absolutely ok for me:
+s
^!1^
i*Q(
}"~
%lh!ghY
#dh!
!`,!
mL#H
o!<XXT0
' <
z !Uk
%
wS
n` !wl
*ty
(Q 6
!XPLO$
E#kF
However, the function gives weird results under python 3. It produces the error:
TypeError: 'in <string>' requires string as left operand, not int
So I converted the 'int' to 'str' by replacing this
if c in string.printable:
with this
if str(c) in string.printable:
(I also converted all the places where the same error message is thrown)
Now the python 3 gives the following output:
56700
0000000000000000000000000000000000000000
1236
60000
400234
00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
2340
0000
5010
5000
17889
2348
23400000000
5600
I cant see any characters when I use python 3. Any help to get the code working or pointer to the solution is appreciated. All I require is to extract the strings from binary file (very small with few kb) and store it in a variable.