Search code examples
pythonutf-16

Matching lines of text in a UTF16-LE file


I'm parsing a file that's written in UTF16-LE for fairly simple matches and none of them seem to trigger. For example, I have the following code.

with open(filepath) as f:
   for line in f:
      if 'TEST_CASE' in line:
         print(line)

Is there a good way to check the lines in the file for matches without altering the file itself?


Solution

  • As per my comment codecs.open('filename', encoding='utf-16-le')