0

I am facing problem when I try to write information to the file using .format().

I am trying to write() text that has been read from text file to another file and then write information about every line:

  • {0} - the number of lines

  • {1} - how many space symbols the line has

  • {2} - how many total symbols the line has

def Spausdinti_Faile(duom, tarpai_kiek, simboliai_kiek):
    with open("rez.txt", "w") as fout:

        i = 0
        for i in range(len(duom)):
            fout.write(duom[i])

        print(tarpai_kiek, simboliai_kiek)
        i = 0
        for i in range(len(duom)):
            fout.write("{0} eilutė turi {1} tarpų simbolių ir {2} 
            simbolių.".format(str(i + 1), str(tarpai_kiek[i]), 
            str(simboliai_kiek[i])))

I get this error:

enter image description here

wovano
  • 4,543
  • 5
  • 22
  • 49
  • 1
    Please supply the expected [minimal, reproducible example](https://stackoverflow.com/help/minimal-reproducible-example). Off-site links and images of text are not acceptable; we need your question to be self-contained, in keeping with the purpose of this site. – Prune Nov 19 '20 at 18:25
  • 1
    @Prune While the image of an error isn't optimal, the problem is reproducible easily enough on any Windows box. – AKX Nov 19 '20 at 18:26

1 Answers1

0

Since you're on Windows, the default character encoding for files is... something limited.

Some of the characters in your language can't be represented in that character encoding, so you get an error to that effect.

Open the file with the UTF-8 encoding; it can represent any character:

with open("rez.txt", "w", encoding="UTF-8") as fout:

You can also set the PYTHONUTF8=1 environment variable to have Python default to UTF-8 on Windows -- it's already the default on other platforms.

AKX
  • 152,115
  • 15
  • 115
  • 172
  • 1
    Even on non-Windows systems I think it's wise to explicitly specify the encoding instead of relying on some default setting. (If you previously relied on the system default and now set the `PYTHONUTF8` variable, the behavior of the old applications changes as well, which might lead to data corruption.) – wovano Nov 19 '20 at 18:46
  • Also note that UTF-8 is not the only option. The format string in the example can also be encoded using 'windows-1257', which is commonly used for the Lithuanian language. It's up to the OP to decide which encoding should be used. But indeed the default encoding in this case is not suitable for this text. – wovano Nov 19 '20 at 18:49
  • For interoperability, UTF-8 is still the best choice in my opinion :) – AKX Nov 19 '20 at 18:52
  • I agree, but I think it's a choice that has to be made consciously. – wovano Nov 19 '20 at 18:56