Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Special chars in Python [duplicate]

Tags:

python

chars

i have to use special chars in my python-application. For example: ƃ I have information like this:

U+0183 LATIN SMALL LETTER B WITH TOPBAR

General Character Properties

In Unicode since: 1.1
Unicode category: Letter, Lowercase

Various Useful Representations

UTF-8: 0xC6 0x83
UTF-16: 0x0183

C octal escaped UTF-8: \306\203
XML decimal entity: &# 387;

But when i just pust symbols into python-script i get an error:

Non-ASCII character '\xc8' ... How can i use it in strings for my application?

like image 897
Max Frai Avatar asked Jun 29 '26 23:06

Max Frai


2 Answers

You should tell the interpreter which encoding you're using, because apparently on your system it defaults to ascii. See PEP 263. In your case, place the following at the top of your file:

# -*- coding: utf-8 -*-

Note that you don't have to write exactly that; PEP 263 allows more freedom, to accommodate several popular editors which use slightly different conventions for the same purpose. Additionally, this string may also be placed on the second line, e.g. when the first is used for the shebang.

like image 105
Stephan202 Avatar answered Jul 02 '26 12:07

Stephan202


http://docs.python.org/tutorial/interpreter.html#source-code-encoding

like image 23
Christopher Avatar answered Jul 02 '26 13:07

Christopher



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!