Answer the question
In order to leave comments, you need to log in
How to change file encoding in python to UNF-8-BOM without \ufeff?
I am writing a script that will automatically translate the game. All localization of the game is in a file with utf-8-bom encoding. I'm translating and writing the translation to a file, which should also be utf-8-bom, but python insists on making it plain utf-8, to convert it to bom I created a function like this:
def encod_utf8_bom(self, path_on_file: str):
file = open(path_on_file, encoding='utf-8', mode='r')
encoding_file = [line.encode('utf-8-sig') for line in file]
file.close()
file = open(path_on_file, 'wb')
[file.write(line) for line in encoding_file]
file.close()
Answer the question
In order to leave comments, you need to log in
\ufeff
According to the Unicode specification, a marker can only appear at the very beginning of a file or stream.
import shutil
def encode_utf8_bom(self, path_on_file: str):
with open(path_on_file, encoding="utf-8") as f_in, open(path_on_file+".tmp", encoding="utf-8-sig", mode="w") as f_out:
f_out.write(f_in.read())
shutil.move(path_on_file + ".tmp", path_on_file)
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question