1

I just studied some Python basic knowledge for a few weeks. And I am a translator so I need to global search and edit Passolo files.

But I failed to search target words in binary mode with Python. Could anyone help to explain what's wrong?

Here is my script:

 path=r'C:\Users\Edwin\Downloads\Temp'
 targetfile=r'C:\Users\Edwin\Downloads\Temp\target.tbulic11'
 key=input("Please entry search key: ").encode()
 print(key)
 content=open(targetfile, 'rb').readlines()
 for line in content:
     if key in line:
        print("Found!!!!!!!!!!!!!!!")
     else:
        print("Bad luck!")
Steve Barnes
  • 27,618
  • 6
  • 63
  • 73
Edwin Hong
  • 11
  • 1
  • Well you can't read lines in a binary file, as there is no such thing as a line. Take a look here about handling binaries in Python: http://stackoverflow.com/questions/1035340/reading-binary-file-in-python – meskobalazs Jan 28 '15 at 09:14

2 Answers2

0

Rather than opening your files in binary mode you need to open them as Unicode files:

import codecs

infile = codecs.open(targetfile)

You will probaly also need to encode your search string to the same encoding as the binary file, this is available as infile..encoding.

Steve Barnes
  • 27,618
  • 6
  • 63
  • 73
0

As mentioned - these are binary files and you can't read lines. The english & translation strings may not be visible.

You have two options to do what you want:

  1. Use the built in BASIC script engine
  2. Use the COM interface provided by Passolo in Python Code.

Both will require you to understand the Script engine components that Passolo provides - so to start with I recommend using BASIC just to prove you can do what you are trying to do. If that works (and you figure out how to access the information you need) - then you can look into using the COM scripting engine.

markm
  • 896
  • 9
  • 13