Error trying to write accents in files with Python

1

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

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

The text was very random, because it was just for testing, 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 for some accent or ç within the .write() parameter. Can anyone help me solve this? Maybe there is some way to change the codec?

    
asked by anonymous 13.04.2017 / 20:57

4 answers

0

Doing a few more tests on Shell, he ended up giving me a little help. When I typed 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 could not do the encode of the characters, it ended up being very simple to solve, it was only to change the argument encoding :

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

Just keep buffering with its default value = -1, and change encoding to utf-8 , which is the default used in Brazil. At the time of reading what was written in the file, I had to do the same thing (only replacing "w" with "r" or "a" ), because if not a bizarre deal:

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

Many thanks to everyone who left their answers, I'm sure they will be useful if anyone else has a similar problem!

    
14.04.2017 / 06:25
1

try the following:

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

I had a similar problem today and I resolved that, I hope I have helped.

    
14.04.2017 / 05:09
0

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

#-*- coding:utf-8 -*-
    
13.04.2017 / 21:02
0

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

    
15.04.2017 / 21:15