D
D
Dmitry Prilepsky2021-07-18 00:36:40
Python
Dmitry Prilepsky, 2021-07-18 00:36:40

How to encrypt string to utf-8-bom in python?

There is such a function. I need to convert a small file from UTF-8 to UTF-8 WITH BOM

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-bom') for line in file]
        file.close()
        file = open(path_on_file, 'wb')
        [file.write(line) for line in encoding_file]
        file.close()

But he does not understand what utf-8-bom is and crashes with an error. The official documentation only says how to read from this encoding, but not how to write it. Can you suggest how this can be done?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
R
Rustam, 2021-07-18
@Rustam

Use 'utf-8-sig' - https://docs.python.org/3/library/codecs.html#modu...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question