-2

I have been having lots of trouble with encondings in python. While searching the net for answers, I have come upon many replies, but none of them actually worked. Then, I found out this behaviour: running the commands in the python interpreter behaves different from actually running the script. See:

$ cat prueba.py 
print("{} con leche".format('Café'))

$ python prueba.py 
  File "prueba.py", line 1
SyntaxError: Non-ASCII character '\xc3' in file prueba.py on line 1, but no encoding declared; see http://python.org/dev/peps/pep-0263/ for details

$ python
Python 2.7.12+ (default, Sep  1 2016, 20:27:38) 
[GCC 6.2.0 20160927] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> print("{} con leche".format('Café'))
Café con leche
>>> 

Can anyone explain this to me? (and, if possible, also help me with the encoding problem)

ilvidel
  • 322
  • 1
  • 4
  • 13
  • 3
    Did you follow the link in the error message, which explains it fully? – Daniel Roseman Oct 29 '16 at 19:43
  • Possible duplicate of [Why does Python print unicode characters when the default encoding is ASCII?](http://stackoverflow.com/questions/2596714/why-does-python-print-unicode-characters-when-the-default-encoding-is-ascii) – Aurora0001 Oct 29 '16 at 19:44

1 Answers1

2

you must add encoding in your script:

# -*- coding: utf-8 -*-
print("{} con leche".format('Café'))
Anton
  • 504
  • 2
  • 6