0

how can I write a file with codepage 1252 in Python on a Raspberry Pi?

I already tried this:

    data = "Testdata"
    with open('somefile', 'w', encoding= 'cp1252') as f:
        f.write(data)

But it creates a file in the utf-8 format.

Any kind of feedback is always appreciated.

Michael
  • 13
  • 3
  • How do you _know_ that a file created this way is `utf-8`? Try data above the ASCII range, e.g. `data = "áíéó"`. Then `cp1252` encoded file should be 4 bytes `\xe1\xed\xe9\xf3` while `utf-8` one should be 8 bytes `\xc3\xa1\xc3\xad\xc3\xa9\xc3\xb3`. With `data = "Testdata"`, both encodings give the same bytes `\x54\x65\x73\x74\x64\x61\x74\x61`… – JosefZ Jun 06 '23 at 14:15

0 Answers0