Python论坛  - 讨论区

标题:[python-chinese] Unicode error in Django

2007年05月26日 星期六 08:08

Ben Luo benluo在gmail.com
星期六 五月 26 08:08:35 HKT 2007

Django svn, Windows 2000, Sqlite3, Python2.5

为什么会有Unicode的问题?

  #test.py

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

import sys
import string

from django.core.management import setup_environ

def execute_from_command_line():
    from django.core.management import setup_environ
    sys.path.append("d:/")

    try:
        from teamware_work import settings
    except ImportError:
        print "You don't appear to have a settings file in this directory!"
        print "Please run this from inside a project directory"
        sys.exit()

    setup_environ(settings)
    inputcity()

def inputcity():
    from teamware_work.dailywork.models import City

    f = open('d:/teamware_work/cityall.txt')
    cityall = f.readlines()
    country = u"中国"
    for prov in cityall:
        city = string.split(prov)
        if len(city) < 2:
            city_tmp = City(city_name_cn=city[0], state_name_cn=city[0],
                            country_name_cn=country, country_name_en="Chinese")
            city_tmp.save()
        else:
            for c in city[1:]:
                city_tmp = City(city_name_cn=c, state_name_cn=city[0],
                                country_name_cn=country,
country_name_en="Chinese")
                city_tmp.save()


if __name__ == "__main__":
    execute_from_command_line()

##########################################
# settings.py
# Django settings for teamware project.

DEBUG = True
TEMPLATE_DEBUG = DEBUG

ADMINS = (
    ('Ben Luo', 'benluo在gmail.com'),
)

MANAGERS = ADMINS

DATABASE_ENGINE = 'sqlite3'           # 'postgresql_psycopg2',
'postgresql', 'mysql', 'sqlite3' or 'ado_mssql'.
DATABASE_NAME = 'd:/teamware_work/team.db'             # Or path to
database file if using sqlite3.
DATABASE_USER = ''             # Not used with sqlite3.
DATABASE_PASSWORD = ''         # Not used with sqlite3.
DATABASE_HOST = ''             # Set to empty string for localhost.
Not used with sqlite3.
DATABASE_PORT = ''             # Set to empty string for default. Not
used with sqlite3.

# Local time zone for this installation. Choices can be found here:
# http://www.postgresql.org/docs/8.1/static/datetime-keywords.html#DATETIME-TIMEZONE-SET-TABLE
# although not all variations may be possible on all operating systems.
# If running in a Windows environment this must be set to the same as your
# system time zone.
TIME_ZONE = 'Asia/Shanghai'

# Language code for this installation. All choices can be found here:
# http://www.w3.org/TR/REC-html40/struct/dirlang.html#langcodes
# http://blogs.law.harvard.edu/tech/stories/storyReader$15
LANGUAGE_CODE = 'zh_CN'

SITE_ID = 1

# If you set this to False, Django will make some optimizations so as not
# to load the internationalization machinery.
USE_I18N = True

# Absolute path to the directory that holds media.
# Example: "/home/media/media.lawrence.com/"
MEDIA_ROOT = ''

# URL that handles the media served from MEDIA_ROOT.
# Example: "http://media.lawrence.com"
MEDIA_URL = ''

# URL prefix for admin media -- CSS, JavaScript and images. Make sure to use a
# trailing slash.
# Examples: "http://foo.com/media/", "/media/".
ADMIN_MEDIA_PREFIX = '/media/'

# Make this unique, and don't share it with anybody.
SECRET_KEY = 'e在3#qvplv=%hz81t_#^*c$41*oktt7tt(_在m&wu;(vq6i&3t(13'

# List of callables that know how to import templates from various sources.
TEMPLATE_LOADERS = (
    'django.template.loaders.filesystem.load_template_source',
    'django.template.loaders.app_directories.load_template_source',
#     'django.template.loaders.eggs.load_template_source',
)

MIDDLEWARE_CLASSES = (
    'django.middleware.common.CommonMiddleware',
    'django.contrib.sessions.middleware.SessionMiddleware',
    'django.contrib.auth.middleware.AuthenticationMiddleware',
    'django.middleware.doc.XViewMiddleware',
)

ROOT_URLCONF = 'teamware_work.urls'

TEMPLATE_DIRS = (
    # Put strings here, like "/home/html/django_templates" or
"C:/www/django/templates".
    # Always use forward slashes, even on Windows.
    # Don't forget to use absolute paths, not relative paths.
#    "d:/basf/project/teamware_work/templates",
)

INSTALLED_APPS = (
    'django.contrib.auth',
    'django.contrib.contenttypes',
    'django.contrib.sessions',
    'django.contrib.sites',
    'django.contrib.databrowse',
    'teamware_work.dailywork',
)

#########################################
#dailywork/models.py

from django.db import models

class City(models.Model):
    city_name_en = models.CharField(maxlength=55)
    city_name_cn = models.CharField(maxlength=55)
    state_name_en = models.CharField(maxlength=55)
    state_name_cn = models.CharField(maxlength=55)
    country_name_en = models.CharField(maxlength=55)
    country_name_cn = models.CharField(maxlength=55)

################
#error

d:\teamware_work>python test.py
python test.py
Traceback (most recent call last):
  File "test.py", line 42, in 
    execute_from_command_line()
  File "test.py", line 20, in execute_from_command_line
    inputcity()
  File "test.py", line 33, in inputcity
    city_tmp.save()
  File "c:\python25\lib\site-packages\django\db\models\base.py", line
243, in save
    ','.join(placeholders)), db_values)
  File "c:\python25\lib\site-packages\django\db\backends\util.py",
line 20, in execute
    'sql': sql % params,
UnicodeDecodeError: 'ascii' codec can't decode byte 0xe5 in position
138: ordinal not in range(128)

‣ About this site 
-------------- 下一部分 --------------
一个HTML附件被移除...
URL: http://python.cn/pipermail/python-chinese/attachments/20070526/8d01eb53/attachment.html 

[导入自Mailman归档:http://www.zeuux.org/pipermail/zeuux-python]

2007年05月26日 星期六 08:37

limodou limodou在gmail.com
星期六 五月 26 08:37:33 HKT 2007

可能和
country_name_cn是一个unicode有关,不要前面的u试试。django一般不需要你直接使用unicode(不过现在有一个unicode版本),你只要使用DEFAULT_CHARSET相同编码的字符串应该就可以了。

-- 
I like python!
UliPad <>: http://wiki.woodpecker.org.cn/moin/UliPad
My Blog: http://www.donews.net/limodou

[导入自Mailman归档:http://www.zeuux.org/pipermail/zeuux-python]

2007年05月26日 星期六 10:04

Ben Luo benluo在gmail.com
星期六 五月 26 10:04:12 HKT 2007

自问自答一下。

在 test.py 中改成

country = u"中国".encode("utf-8")

就可以了。

On 5/26/07, Ben Luo <benluo在gmail.com> wrote:
>
> Django svn, Windows 2000, Sqlite3, Python2.5
>
> 为什么会有Unicode的问题?
>
>   #test.py
>
> # -*- coding: utf-8 -*-
>
> import sys
> import
> string
>
> from django.core.management import setup_environ
>
> def execute_from_command_line
> ():
>     from django.core.management import setup_environ
>     sys.path.append("d:/")
>
>
> try:
>         from teamware_work import settings
>     except
> ImportError:
>         print "You don't appear to have a settings file in this directory!"
>         print
>  "Please run this from inside a project directory"
>         sys.exit()
>
>
>     setup_environ(settings)
>     inputcity()
>
> def
>  inputcity():
>     from teamware_work.dailywork.models import City
>
>
>     f = open('d:/teamware_work/cityall.txt')
>     cityall
>  = f.readlines()
>     country = u"中国"
>     for prov in cityall:
>         city
> = string.split(prov)
>         if
> len(city) < 2:
>             city_tmp
> = City(city_name_cn=city[0],
> state_name_cn=city[0],
>                             country_name_cn
> =country, country_name_en="Chinese")
>
> city_tmp.save()
>         else:
>             for
> c in city[1:]:
>                 city_tmp =
> City(city_name_cn=c, state_name_cn=city
> [0],
>                                 country_name_cn=country
> , country_name_en="Chinese")
>                 city_tmp.
> save()
>
>
> if __name__ == "__main__":
>
>     execute_from_command_line()
>
> ##########################################
> # settings.py
> # Django settings for teamware project.
>
>
> DEBUG = True
> TEMPLATE_DEBUG = DEBUG
>
> ADMINS
>  = (
>     ('Ben Luo', 'benluo在gmail.com
> '),
> )
>
> MANAGERS = ADMINS
>
> DATABASE_ENGINE
> = 'sqlite3'           # 'postgresql_psycopg2', 'postgresql', 'mysql', 'sqlite3' or 'ado_mssql'.
> DATABASE_NAME
>  = 'd:/teamware_work/team.db'             # Or path to database file if using sqlite3.
> DATABASE_USER
> = ''             # Not used with sqlite3.
> DATABASE_PASSWORD = ''
> # Not used with sqlite3.
> DATABASE_HOST = ''             # Set to empty string for localhost. Not used with sqlite3.
> DATABASE_PORT = ''             # Set to empty string for default. Not used with sqlite3.
>
> # Local time zone for this installation. Choices can be found here:
>
> # http://www.postgresql.org/docs/8.1/static/datetime-keywords.html#DATETIME-TIMEZONE-SET-TABLE
>
> # although not all variations may be possible on all operating systems.
> # If running in a Windows environment this must be set to the same as your
>
> # system time zone.
> TIME_ZONE = 'Asia/Shanghai'
>
> # Language code for this installation. All choices can be found here:
>
> # http://www.w3.org/TR/REC-html40/struct/dirlang.html#langcodes
> #
> http://blogs.law.harvard.edu/tech/stories/storyReader$15
> LANGUAGE_CODE = 'zh_CN'
>
> SITE_ID
> = 1
>
> # If you set this to False, Django will make some optimizations so as not
> # to load the internationalization machinery.
>
> USE_I18N = True
>
> # Absolute path to the directory that holds media.
> # Example: "/home/media/media.lawrence.com/"
>
> MEDIA_ROOT = ''
>
> # URL that handles the media served from MEDIA_ROOT.
> # Example: "
> http://media.lawrence.com"
> MEDIA_URL = ''
>
> # URL prefix for admin media -- CSS, JavaScript and images. Make sure to use a
>
> # trailing slash.
> # Examples: "http://foo.com/media/", "/media/".
> ADMIN_MEDIA_PREFIX
>  = '/media/'
>
> # Make this unique, and don't share it with anybody.
> SECRET_KEY =
> 'e在3#qvplv=%hz81t_#^*c$41*oktt7tt(_在m&wu;(vq6i&3t(13'
>
> # List of callables that know how to import templates from various sources.
> TEMPLATE_LOADERS
>  = (
>     'django.template.loaders.filesystem.load_template_source',
>     'django.template.loaders.app_directories.load_template_source
> ',
> #     'django.template.loaders.eggs.load_template_source',
> )
>
> MIDDLEWARE_CLASSES
> = (
>     'django.middleware.common.CommonMiddleware',
>     'django.contrib.sessions.middleware.SessionMiddleware'
> ,
>     'django.contrib.auth.middleware.AuthenticationMiddleware',
>     'django.middleware.doc.XViewMiddleware'
> ,
> )
>
> ROOT_URLCONF = 'teamware_work.urls'
>
> TEMPLATE_DIRS
>  = (
>     # Put strings here, like "/home/html/django_templates" or "C:/www/django/templates".
>     # Always use forward slashes, even on Windows.
>
>     # Don't forget to use absolute paths, not relative paths.
> #    "d:/basf/project/teamware_work/templates",
> )
>
>
> INSTALLED_APPS = (
>     'django.contrib.auth',
>     'django.contrib.contenttypes',
>     'django.contrib.sessions',
>     'django.contrib.sites',
>
> 'django.contrib.databrowse',
>     'teamware_work.dailywork',
> )
>
> #########################################
>
> #dailywork/models.py
>
> from django.db import models
>
> class
> City(models.Model):
>     city_name_en =
> models.CharField(maxlength=55)
>
> city_name_cn = models.CharField(maxlength=
> 55)
>     state_name_en = models.CharField(
> maxlength=55)
>     state_name_cn = models.
> CharField(maxlength=55)
>     country_name_en =
> models.CharField(maxlength=55)
>
> country_name_cn = models.CharField(maxlength=
> 55)
>
> ################
> #error
>
> d:\teamware_work
> >python test.py
> python test.py
> Traceback (most recent call last):
>
> File "test.py", line 42, in <
> module>
>     execute_from_command_line()
>   File "test.py",
>  line 20, in execute_from_command_line
>     inputcity()
>
>   File "test.py", line 33, in inputcity
>
>     city_tmp.save()
>   File "c:\python25\lib\site-packages\django\db\models
> \base.py", line 243, in save
>     ','.join(placeholders)), db_values
> )
>   File "c:\python25\lib\site-packages\django\db\backends\util.py",
> line 20, in execute
>     'sql': sql
> % params,
> UnicodeDecodeError: 'ascii' codec can't decode byte 0xe5 in position 138: ordinal not in range(128)
>
> ‣ About this site <http://about/>
>
>
-------------- 下一部分 --------------
一个HTML附件被移除...
URL: http://python.cn/pipermail/python-chinese/attachments/20070526/5c310685/attachment-0001.html 

[导入自Mailman归档:http://www.zeuux.org/pipermail/zeuux-python]

2007年05月26日 星期六 10:25

黄毅 yi.codeplayer在gmail.com
星期六 五月 26 10:25:13 HKT 2007

>
> 在 test.py 中改成
>
> country = u"中国".encode("utf-8")
>
> 就可以了。


晕 这和直接写 "中国" 不一样嘛。

-- 
http://codeplayer.blogspot.com/
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://python.cn/pipermail/python-chinese/attachments/20070526/fb03008b/attachment.htm 

[导入自Mailman归档:http://www.zeuux.org/pipermail/zeuux-python]

2007年05月26日 星期六 10:40

Ben Luo benluo在gmail.com
星期六 五月 26 10:40:01 HKT 2007

On 5/26/07, »ÆÒã <yi.codeplayer在gmail.com> wrote:
>
> ÔÚ test.py ÖиijÉ
> >
> > country = u"Öйú".encode("utf-8")
> >
> > ¾Í¿ÉÒÔÁË¡£
>
>
> ÔÎ ÕâºÍÖ±½Óд "Öйú" ²»Ò»ÑùÂï¡£
>

ÒòΪ¶ÔunicodeµÄÀí½â²»Éî¡£·¢ÁËÌû×Ӳŷ¢ÏÖºÍÄã˵µÄÒ»Ñù¡£×Ô¼º´ÀÁËÒ»°Ñ
-------------- 下一部分 --------------
Ò»¸öHTML¸½¼þ±»ÒƳý...
URL: http://python.cn/pipermail/python-chinese/attachments/20070526/db2d4f55/attachment.html 

[导入自Mailman归档:http://www.zeuux.org/pipermail/zeuux-python]

2007年05月26日 星期六 11:18

黄毅 yi.codeplayer在gmail.com
星期六 五月 26 11:18:41 HKT 2007

用 chunk 的人可以考虑 http://code.djangoproject.com/wiki/UnicodeBranch 分支了。
Unicode-branch: testers
wanted<http://groups.google.com/group/django-users/t/fe48a0b635f59ee>


On 5/26/07, Ben Luo <benluo at gmail.com> wrote:
>
> Django svn, Windows 2000, Sqlite3, Python2.5
>
> 为什么会有Unicode的问题?
>
>    #test.py
>
> # -*- coding: utf-8 -*-
>
> import sys
> import
> string
>
> from django.core.management import setup_environ
>
> def execute_from_command_line():
>     from django.core.management import setup_environ
>     sys.path.append("d:/")
>
>
> try:
>         from teamware_work import settings
>     except
> ImportError:
>         print "You don't appear to have a settings file in this directory!"
>         print
>  "Please run this from inside a project directory"
>         sys.exit()
>
>
>     setup_environ(settings)
>     inputcity()
>
> def
>  inputcity():
>     from teamware_work.dailywork.models import City
>
>     f = open('d:/teamware_work/cityall.txt')
>     cityall = f.readlines()
>     country = u"中国"
>     for prov in cityall:
>         city = string.split(prov)
>         if
> len(city) < 2:
>             city_tmp
> = City(city_name_cn=city[0], state_name_cn=city[0],
>                             country_name_cn=country, country_name_en="Chinese")
>             city_tmp.save()
>         else:
>             for
> c in city[1:]:
>                 city_tmp =
> City(city_name_cn=c, state_name_cn=city[0],
>                                 country_name_cn=country
> , country_name_en="Chinese")
>                 city_tmp.
> save()
>
>
> if __name__ == "__main__":
>     execute_from_command_line()
>
> ##########################################
> # settings.py
> # Django settings for teamware project.
>
> DEBUG = True
> TEMPLATE_DEBUG = DEBUG
>
> ADMINS = (
>     ('Ben Luo', '
> benluo at gmail.com'),
> )
>
> MANAGERS = ADMINS
>
> DATABASE_ENGINE
> = 'sqlite3'           # 'postgresql_psycopg2', 'postgresql', 'mysql', 'sqlite3' or 'ado_mssql'.
> DATABASE_NAME = 'd:/teamware_work/team.db'             # Or path to database file if using sqlite3.
> DATABASE_USER
> = ''             # Not used with sqlite3.
> DATABASE_PASSWORD = ''
> # Not used with sqlite3.
> DATABASE_HOST = ''             # Set to empty string for localhost. Not used with sqlite3.
> DATABASE_PORT = ''             # Set to empty string for default. Not used with sqlite3.
>
> # Local time zone for this installation. Choices can be found here:
> # http://www.postgresql.org/docs/8.1/static/datetime-keywords.html#DATETIME-TIMEZONE-SET-TABLE
> # although not all variations may be possible on all operating systems.
> # If running in a Windows environment this must be set to the same as your
>
> # system time zone.
> TIME_ZONE = 'Asia/Shanghai'
>
> # Language code for this installation. All choices can be found here:
> # http://www.w3.org/TR/REC-html40/struct/dirlang.html#langcodes
> #
> http://blogs.law.harvard.edu/tech/stories/storyReader$15
> LANGUAGE_CODE = 'zh_CN'
>
> SITE_ID
> = 1
>
> # If you set this to False, Django will make some optimizations so as not
> # to load the internationalization machinery.
>
> USE_I18N = True
>
> # Absolute path to the directory that holds media.
> # Example: "/home/media/media.lawrence.com/"
> MEDIA_ROOT = ''
>
> # URL that handles the media served from MEDIA_ROOT.
> # Example: "http://media.lawrence.com"
> MEDIA_URL = ''
>
> # URL prefix for admin media -- CSS, JavaScript and images. Make sure to use a
> # trailing slash.
> # Examples: "http://foo.com/media/", "/media/".
> ADMIN_MEDIA_PREFIX
>  = '/media/'
>
> # Make this unique, and don't share it with anybody.
> SECRET_KEY =
> 'e at 3#qvplv=%hz81t_#^*c$41*oktt7tt(_ at m&wu;(vq6i&3t(13'
>
> # List of callables that know how to import templates from various sources.
> TEMPLATE_LOADERS
>  = (
>     'django.template.loaders.filesystem.load_template_source',
>     'django.template.loaders.app_directories.load_template_source
> ',
> #     'django.template.loaders.eggs.load_template_source',
> )
>
> MIDDLEWARE_CLASSES
> = (
>     'django.middleware.common.CommonMiddleware',
>     'django.contrib.sessions.middleware.SessionMiddleware',
>     'django.contrib.auth.middleware.AuthenticationMiddleware',
>     'django.middleware.doc.XViewMiddleware',
> )
>
> ROOT_URLCONF = 'teamware_work.urls'
>
> TEMPLATE_DIRS
>  = (
>     # Put strings here, like "/home/html/django_templates" or "C:/www/django/templates".
>     # Always use forward slashes, even on Windows.
>     # Don't forget to use absolute paths, not relative paths.
> #    "d:/basf/project/teamware_work/templates",
> )
>
>
> INSTALLED_APPS = (
>     'django.contrib.auth',
>     'django.contrib.contenttypes',
>     'django.contrib.sessions',
>     'django.contrib.sites',
>
> 'django.contrib.databrowse',
>     'teamware_work.dailywork',
> )
>
> #########################################
> #dailywork/models.py
>
> from django.db import models
>
> class
> City(models.Model):
>     city_name_en =
> models.CharField(maxlength=55)
>
> city_name_cn = models.CharField(maxlength=
> 55)
>     state_name_en = models.CharField(
> maxlength=55)
>     state_name_cn = models.
> CharField(maxlength=55)
>     country_name_en =
> models.CharField(maxlength=55)
>
> country_name_cn = models.CharField(maxlength=
> 55)
>
> ################
> #error
>
> d:\teamware_work
> >python test.py
> python test.py
> Traceback (most recent call last):
>
> File "test.py", line 42, in <
> module>
>     execute_from_command_line()
>   File "test.py",
>  line 20, in execute_from_command_line
>     inputcity()
>
>   File "test.py", line 33, in inputcity
>     city_tmp.save()
>   File "c:\python25\lib\site-packages\django\db\models\base.py", line 243, in save
>     ','.join(placeholders)), db_values
> )
>   File "c:\python25\lib\site-packages\django\db\backends\util.py",
> line 20, in execute
>     'sql': sql
> % params,
> UnicodeDecodeError: 'ascii' codec can't decode byte 0xe5 in position 138: ordinal not in range(128)
>
> ‣ About this site <http://about/>
>
>
> _______________________________________________
> python-chinese
> Post: send python-chinese at lists.python.cn
> Subscribe: send subscribe to python-chinese-request at lists.python.cn
> Unsubscribe: send unsubscribe to  python-chinese-request at lists.python.cn
> Detail Info: http://python.cn/mailman/listinfo/python-chinese
>



-- 
http://codeplayer.blogspot.com/
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://python.cn/pipermail/python-chinese/attachments/20070526/06db7a36/attachment-0001.htm 

[导入自Mailman归档:http://www.zeuux.org/pipermail/zeuux-python]

2007年05月26日 星期六 11:27

黄毅 yi.codeplayer在gmail.com
星期六 五月 26 11:27:55 HKT 2007

>
> django一般不需要你直接使用unicode(不过现在有一个unicode版本),


Unicode 分支也不是说用户代码一定要使用unicode,不过 django 内部一律都用 unicode 交流。

Note that we are not trying to switch to forcing everybody to *only* use
unicode strings. You will also be able to pass around bytestrings and Django
will assume they are UTF-8
encoded<http://code.djangoproject.com/wiki/UnicodeBranch>

-- 
http://codeplayer.blogspot.com/
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://python.cn/pipermail/python-chinese/attachments/20070526/3b1e0d8a/attachment.html 

[导入自Mailman归档:http://www.zeuux.org/pipermail/zeuux-python]

如下红色区域有误,请重新填写。

    你的回复:

    请 登录 后回复。还没有在Zeuux哲思注册吗?现在 注册 !

    Zeuux © 2025

    京ICP备05028076号