Fix Python – Character reading from file in Python

In a text file, there is a string “I don’t like this”.
However, when I read it into a string, it becomes “I don\xe2\x80\x98t like this”. I understand that \u2018 is the unicode representation of “‘”. I use
f1 = open (file1, “r”)
text = f1.read()

command to do the reading.
Now, is it possible to read the string in such a way that when it is read ….

Fix Python – Python – ‘ascii’ codec can’t decode byte

I’m really confused. I tried to encode but the error said can’t decode….
>>> “你好”.encode(“utf8”)
Traceback (most recent call last):
File ““, line 1, in
UnicodeDecodeError: ‘ascii’ codec can’t decode byte 0xe4 in position 0: ordinal not in range(128)

I know how to avoid the error with “u” prefix on the string. I’m just wondering….

Fix Python – Why does Python print unicode characters when the default encoding is ASCII?

From the Python 2.6 shell:
>>> import sys
>>> print sys.getdefaultencoding()
ascii
>>> print u’\xe9′
é
>>>

I expected to have either some gibberish or an Error after the print statement, since the “é” character isn’t part of ASCII and I haven’t specified an encoding. I guess I don’t understand what ASCII being the default encoding means.
EDIT
I ….

Fix Python – Python: Using .format() on a Unicode-escaped string

I am using Python 2.6.5. My code requires the use of the “more than or equal to” sign. Here it goes:
>>> s = u’\u2265′
>>> print s
>>> ≥
>>> print “{0}”.format(s)
Traceback (most recent call last):
File ““, line 1, in
UnicodeEncodeError: ‘ascii’ codec can’t encode character u’\u2265′
in position 0: ordinal not in range(12….

Fix Python – Python string prints as [u’String’]

This will surely be an easy one but it is really bugging me.
I have a script that reads in a webpage and uses Beautiful Soup to parse it. From the soup I extract all the links as my final goal is to print out the link.contents.
All of the text that I am parsing is ASCII. I know that Python treats strings as unicode, and I am sure this is very han….

Fix Python – Python, Unicode, and the Windows console

When I try to print a Unicode string in a Windows console, I get an error .
UnicodeEncodeError: ‘charmap’ codec can’t encode character ….
I assume this is because the Windows console does not accept Unicode-only characters. What’s the best way around this?
Is there any way I can make Python automatically print a ? instead of failing in this situ….

Fix Python – MySQL “incorrect string value” error when save unicode string in Django

I got strange error message when tried to save first_name, last_name to Django’s auth_user model.
Failed examples
user = User.object.create_user(username, email, password)
user.first_name = u’Rytis’
user.last_name = u’Slatkevičius’
user.save()
>>> Incorrect string value: ‘\xC4\x8Dius’ for column ‘last_name’ at row 104

user.first_name = u’Валерий’….

Fix Python – NameError: global name ‘unicode’ is not defined – in Python 3

I am trying to use a Python package called bidi. In a module in this package (algorithm.py) there are some lines that give me error, although it is part of the package.
Here are the lines:
# utf-8 ? we need unicode
if isinstance(unicode_or_str, unicode):
text = unicode_or_str
decoded = False
else:
text = unicode_or_str.decode(encoding)….