3
I’m writing a program and it keeps giving ascii error.
I’ve put this on the first line and nothing:
# -*- coding: utf-8 -*-
On request, follows edition explaining what is now.
now is variable for datetime and I believe it has no relation.
The accent on the console (terminal) is normal. What does not work is because I put to write in a file . txt and when put to write with accent that appears this error.
I did what mgibsonbr asked and gave everything ok. Use OS X.
So, by acting out:
- now is a variable for datetime that has nothing to do with accentuation (I believe);
- Accent error only appears when I write to a file. txt;
Could put the part of the code that gives error?
– Felipe Avelar
The code I want to put accent is this:
f.write('- %s/%s/%s às %s:%s -\n' % (now.day, now.month, now.year, now.hour, now.minute))
And the error I get is this:;Traceback (most recent call last): 
 File "/Users/gabrielazevedo/Meus Trabalhos/Python/Calculadora de Imposto e Gorjeta.py", line 32, in <module> 
 f.write('- %s/%s/%s às %s:%s -\n' % (now.day, now.month, now.year, now.hour, now.minute)) 
UnicodeEncodeError: 'ascii' codec can't encode character '\xe0' in position 12: ordinal not in range(128)
– Gabriel Azevedo
Edit your issue with the code and, if possible, put what is the now. (:
– Felipe Avelar
Dumb question, but are you sure your source file is encoded as UTF-8, right? To be sure, try putting
x = "á"
in the second line of the archive andprint("ok")
in the third, if he nay print "ok" so probably your source file is with the wrong encoding (if you are on Windows, it is probablyCp1252
).– mgibsonbr
I edited it for you to read
– Gabriel Azevedo