“SyntaxError: Non-ASCII character …” or “SyntaxError: Non-UTF-8 code starting with …” trying to use non-ASCII text in a Python script

I’d recommend reading that PEP the error gives you. The problem is that your code is trying to use the ASCII encoding, but the pound symbol is not an ASCII character. Try using UTF-8 encoding. You can start by putting # -*- coding: utf-8 -*- at the top of your .py file. To get more … Read more

Python 3: os.walk() file paths UnicodeEncodeError: ‘utf-8’ codec can’t encode: surrogates not allowed

On Linux, filenames are ‘just a bunch of bytes’, and are not necessarily encoded in a particular encoding. Python 3 tries to turn everything into Unicode strings. In doing so the developers came up with a scheme to translate byte strings to Unicode strings and back without loss, and without knowing the original encoding. They … Read more

Python string argument without an encoding

You are passing in a string object to a bytearray(): bytearray(content[current_pos:(final_pos)]) You’ll need to supply an encoding argument (second argument) so that it can be encoded to bytes. For example, you could encode it to UTF-8: bytearray(content[current_pos:(final_pos)], ‘utf8’) From the bytearray() documentation: The optional source parameter can be used to initialize the array in a … Read more

Python – ‘ascii’ codec can’t decode byte

“你好”.encode(‘utf-8′) encode converts a unicode object to a string object. But here you have invoked it on a string object (because you don’t have the u). So python has to convert the string to a unicode object first. So it does the equivalent of “你好”.decode().encode(‘utf-8′) But the decode fails because the string isn’t valid ascii. … Read more

How to print Unicode character in Python?

To include Unicode characters in your Python source code, you can use Unicode escape characters in the form \u0123 in your string. In Python 2.x, you also need to prefix the string literal with ‘u’. Here’s an example running in the Python 2.x interactive console: >>> print u’\u0420\u043e\u0441\u0441\u0438\u044f’ Россия In Python 2, prefixing a string … Read more

UnicodeDecodeError: ‘utf8’ codec can’t decode byte 0x9c

http://docs.python.org/howto/unicode.html#the-unicode-type str = unicode(str, errors=”replace”) or str = unicode(str, errors=”ignore”) Note: This will strip out (ignore) the characters in question returning the string without them. For me this is ideal case since I’m using it as protection against non-ASCII input which is not allowed by my application. Alternatively: Use the open method from the codecs … Read more

UnicodeEncodeError: ‘ascii’ codec can’t encode character u’\xa0′ in position 20: ordinal not in range(128)

You need to read the Python Unicode HOWTO. This error is the very first example. Basically, stop using str to convert from unicode to encoded text / bytes. Instead, properly use .encode() to encode the string: p.agent_info = u’ ‘.join((agent_contact, agent_telno)).encode(‘utf-8’).strip() or work entirely in unicode.

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)