[Python] 'charmap' codec can't encode characters in position 214-215: character maps to...

Discussão em 'Python' iniciado por Stack, Setembro 29, 2024.

  1. Stack

    Stack Membro Participativo

    i am trying to translate a text english from turkish using googletrans library and simply print it. But sometimes i get this error. I couldn't solve it.

    code is:

    lin = translator.translate(line, dest="tr", src="en")
    print(lin)


    exact error:

    Traceback (most recent call last):
    File "location", line 138, in <module>
    googletrans()
    File "location", line 135, in googletrans
    print(line)
    File "location", line 19, in encode
    return codecs.charmap_encode(input,self.errors,encoding_table)[0]
    UnicodeEncodeError: 'charmap' codec can't encode characters in position 214-215: character maps to <undefined>


    note to moderators: There is another question like this but it is for open("file","w") command and it is from 2013 it doesnt work.

    i simply wanted to print a string to terminal. I can't even see what it can't print. Is there a way to do that also?

    edit: it looks like translate outputs some zero width space. word is this: \u200b\u200bbağımlılığını. repr(line) can print it but regular print still can't

    edit2: simply using print(line.replace("\u200b", "")) works but i still have no idea why translate adds randomly zero width character. My eng string looks like doesnt have any zero width space.

    Continue reading...

Compartilhe esta Página