0

After writing to a txt file from site, special characters are replaced by the � symbol. How can I write the original letters to the file correctly?

Kins
  • 547
  • 1
  • 5
  • 22

2 Answers2

1

Make sure to specify the encoding when opening the file:

with open("file.txt", encoding="utf-8") as f:
    f.write("ąęćż")

Works properly on windows. On other operating systems UTF-8 is default, which is very confusing.

mousetail
  • 7,009
  • 4
  • 25
  • 45
0

Please check that your text editor also supports the encoding in order to view the actual characters on screen. You might be writing correctly to the file, but your text editor is unable to display special characters.

ngrj93
  • 1
  • 1