M.Mazzaz
M.Mazzaz

Reputation: 1

read from a txt file with encoding= cp1256

I am trying to read and extract data from a file with encoding =cp1256 I can read the file and print all the information form it, but if I tried to search for something using the line.startswith it is not working

printing = False
with open(SourceFile,"r") as file:
    for line in file:
        if line.startswith("NODes\n"): # search for a keyword
            printing = True
            continue # go to next line
        elif line.startswith(";CON"):
            printing = False
            break #quit file reading
        if printing:
            print(line, file=PointsFile)
PointsFile.close()

it is only working if I save the file using the notepad and change the encoding to utf-8 the same code works fine what should I do to make it works without changing the encoding

Upvotes: 0

Views: 93

Answers (1)

Daweo
Daweo

Reputation: 36360

open has optional argument encoding, codecs - Standard Encodings shows table of encodings, as cp1256 is one of them it should suffice to replace

with open(SourceFile,"r") as file:

using

with open(SourceFile,"r",encoding="cp1256") as file:

Upvotes: 2

Related Questions