WebFaction
Community site: login faq

I have a problem with the default encoding in Python scripts. Do I understand correctly that the default encoding is set in /usr/local/lib/python2.7/site.py, which imports automatically?

def setencoding():
    encoding = "ascii"
    if 0:
        loc = locale.getdefaultlocale()
        if loc[1]:
            encoding = loc[1]
    if 0:
        encoding = "undefined"
    if encoding != "ascii":
        sys.setdefaultencoding(encoding)
>>> import sys
>>> sys.getdefaultencoding() 
... 'ascii'

Because of this, any use of print with unicode string raises:

UnicodeEncodeError: 'ascii' codec can't encode characters in position 0-4: ordinal not in range(128)

What am I doing wrong and how to do it right?

asked 14 Mar '11, 04:33

klesh
1112
accept rate: 0%


The default encoding in Python will be ascii unless otherwise defined. This is normally done at the beginning of your python source with a line like:

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

The official documentation for encodings in Python is available at:

http://docs.python.org/howto/unicode.html

permanent link

answered 14 Mar '11, 04:54

ryans ♦♦
5.0k93360
accept rate: 43%

I know about "coding: utf-8". Here is test script:

#!/usr/local/bin/python2.7
# -*- coding: utf-8 -*-

print "Content-type: text/html; charset=utf-8nn"

import cgitb cgitb.enable()

import MySQLdb db=MySQLdb.connect(passwd='***', user='' , db='', charset='utf8', use_unicode=True ) c=db.cursor()

c.execute("select title from inmeet_tags") a = c.fetchall() for x in a: print x[0]

From ssh console: OK, unicode lines displays correctly.

Over http: Traceback (most recent call last): File "uni.py", line 16, in <module> print x[0] UnicodeEncodeError: 'ascii' codec can't encode characters in position 0-4: ordinal not in range(128)

permanent link

answered 14 Mar '11, 05:09

klesh
1112
accept rate: 0%

edited 14 Mar '11, 08:32

Thanks for answers. Resolved by adding reload(sys).

import sys
reload(sys)
sys.setdefaultencoding('utf-8')

permanent link

answered 14 Mar '11, 09:18

klesh
1112
accept rate: 0%

Your answer
toggle preview

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Markdown Basics

  • *italic* or _italic_
  • **bold** or __bold__
  • link:[text](http://url.com/ "title")
  • image?![alt text](/path/img.jpg "title")
  • numbered list: 1. Foo 2. Bar
  • to add a line break simply add two spaces to where you would like the new line to be.
  • basic HTML tags are also supported

Question tags:

×337
×7
×2
×1

question asked: 14 Mar '11, 04:33

question was seen: 13,225 times

last updated: 14 Mar '11, 09:18

WEBFACTION
REACH US
SUPPORT
AFFILIATE PROGRAM
LEGAL
© COPYRIGHT 2003-2019 SWARMA LIMITED - WEBFACTION IS A SERVICE OF SWARMA LIMITED
REGISTERED IN ENGLAND AND WALES 5729350 - VAT REGISTRATION NUMBER 877397162
5TH FLOOR, THE OLD VINYL FACTORY, HAYES, UB3 1HA, UNITED KINGDOM