Error while trying to write accents in Python files

Asked

Viewed 3,447 times

1

I was trying to make a program in Python that could make changes to another . py file in order to change its code automatically. I went to do the following test on Shell before writing the program itself:

>>> import os
>>> file = open(os.path.abspath('.') + 'outroarquivo.py', 'a+')
>>> file.write("Quero caféééééééé!")

The text was quite random, because it was only to test, but this last line gave the following return:

Traceback (most recent call last):
  File "<pyshell#37>", line 1, in <module>
    file.write("Quero caféééééééé!")
UnicodeEncodeError: 'cp932' codec can't encode character '\xe9' in position 9: illegal multibyte sequence

This occurs whenever I try to put some accent or ç within the parameter of .write(). Can anyone help me solve this? Maybe there is some way to change the codec?

  • Search stackoverflow in English- these accentuation questions often appear. And above all, read http://local.joelonsoftware.com/wiki/O_M%C3%Adnimo_absoluto_que_todos_programmeres_de_software_they need,_Absolutely,Positivamente_de_Saber_Sobre_Unicode_e_Conjuntos_de_Caracteres(Apologies!)

4 answers

1

try the following:

str(file.write("Quero caféééééééé!")).encode(encoding='UTF-8',errors='strict')

I had a similar problem today and solved so I hope I helped.

0

If running the program on Windows, prefer cp1252 instead of UTF-8 in the functional comment of character encoding; works better.

0


Doing some more tests in Shell, he ended up giving me a little help. When I went to type the command open, the program showed the parameters of this function:

>>> open(filse, mode='r', buffering=-1, encoding=None)

Since the problem is with the codec cp932 that I could not do the Encounter of the characters, ended up being very simple to solve, just change the argument encoding:

>>> file = open(os.path.abspath(".") + "\\outroarquivo.py", "w", -1, "utf-8")
>>> file.write("Quero caféééééééé!")

I was just keeping the buffering with its default value = -1, and change the encoding to the utf-8, which is the standard used in Brazil. At the time of reading what was written in the file, I had to do the same thing (just replacing "w" for "r" or "a"), 'Cause if a bizarre deal doesn’t go down:

>>> file = open(os.path.abspath(".") + "\\outroarquivo.py", "r")
>>> ler = file.read()
>>> ler
'Quero cafテゥテゥテゥテゥテゥテゥテゥテゥ!'

Thank you very much to everyone who left their answers, I’m sure they’ll be helpful if anyone else has a similar problem!

0

Try adding the encoding statement at the beginning of the . py file

#-*- coding:utf-8 -*-
  • This for the file where you will run the open.

  • 1

    Declaring encoding is not "magic". It solves nothing in the case.

  • But I said it was magic? I reproduced his problem and solved it.

  • I tested it on my pc and kept giving error. Maybe we use different interpreters or whatever, so it might still work for other people if they see a similar mistake. I’ve been taking a look at the net, and from what I understand this first line you suggested serves more to indicate the encoding of the program’s own code than the encoding of the files it opens and manipulates. Anyway, thank you both so much for your concern in solving the problem!

  • 1

    You did not "reproduce his problem"- if you fail to paste the encoding into a Python 2 file, you have a "Sntaxerror" - his error was "Unicodeencorror". If you don’t know what’s going on, you’re trying to solve by trial and error: so it’s "magic".

  • jsbueno, you’re right, I could only reproduce a syntax error, probably his problem is more complex, sorry for the mistake. I saw you left an article about encoding. Really these problems when I come across I go by trial and error even, but I will give a read to better understand how to solve. Thanks and sorry again !

Show 1 more comment

Browser other questions tagged

You are not signed in. Login or sign up in order to post.