character like ö, ü, ä, ß

How can I run a syntax like:

print 'ö'

I get the answer:

SyntaxError: Non-ASCII character '\xc3' in file

I need the character ö, ü, ä, ß. I changed the encoding to UTF-8 (settings -> file encoding) but it doesn't run.

I hope anywhere can help.



Win 7
PyCharm 3.0.1
0
2 comments
Welcome to the Python character encoding hell! The error means that you're having non-ascii characters in your source file. Python assumes that your source files are in ascii encoding unless you explicitly declare an encoding for a file.

You can enter a comment at the top of your file: # coding=utf-8
That sets the file encoding to UTF-8. If you're currently using something else, there's an encoding indicator in the status bar. If you select the desired encoding there, PyCharm will ask whether it should assume that the file already has that encoding or whether it should be converted into that encoding.

In case you're wondering why that indicator is sometimes disabled: that happens when PyCharm detects such a special comment that declares the character encoding.

Then, you can start printing umlauts: print u'ä' or print 'ä' (in Python 3.x).

You'll find more information here: Unicode Howto. Strings in Python 3 are different from those in 2.7 in regard to unicode handling: Python 2.7 assumes actual bytes, Python 3.x assumes they contain unicode code points. Please note, that technically you'll have to make sure that the characters printed are converted to the correct encoding of your console if those two do not match (On windows, a cmd windows probably has some windows encoding and not utf-8).

Hope that helps.
0
It works ... Thank you!
0

Please sign in to leave a comment.