2015-05-04 93 views
0

我試圖從xml文件中用cyrilic字符獲取產品目錄。我ElementTree的解析它,獲得了數據,並試圖將其保存到數據庫中,但我得到了一個編碼錯誤:Django:解析xml ElementTree時的UnicodeEncodeError

Request Method: GET 
Request URL: http://detsad-komplekt.loc/catalog/commerce/ 

Django Version: 1.8 
Python Version: 2.7.6 
Installed Applications: 
(u'admin_tools', 
u'admin_tools.theming', 
u'admin_tools.menu', 
u'admin_tools.dashboard', 
u'django.contrib.admin', 
u'django.contrib.auth', 
u'django.contrib.contenttypes', 
u'django.contrib.sessions', 
u'django.contrib.messages', 
u'django.contrib.staticfiles', 
u'mptt', 
u'daguerre', 
u'ckeditor', 
u'home', 
u'accounts', 
u'catalog', 
'debug_toolbar', 
'coverage') 
Installed Middleware: 
(u'debug_toolbar.middleware.DebugToolbarMiddleware', 
u'django.contrib.sessions.middleware.SessionMiddleware', 
u'django.middleware.common.CommonMiddleware', 
u'django.middleware.csrf.CsrfViewMiddleware', 
u'django.contrib.auth.middleware.AuthenticationMiddleware', 
u'django.contrib.auth.middleware.SessionAuthenticationMiddleware', 
u'django.contrib.messages.middleware.MessageMiddleware', 
u'django.middleware.clickjacking.XFrameOptionsMiddleware', 
u'django.middleware.security.SecurityMiddleware') 


Traceback: 
File "/home/oleshko/design/.virtualenvs/detsad-komplekt/local/lib/python2.7/site-packages/django/core/handlers/base.py" in get_response 
    132.      response = wrapped_callback(request, *callback_args, **callback_kwargs) 
File "/home/oleshko/design/.virtualenvs/detsad-komplekt/local/lib/python2.7/site-packages/django/views/generic/base.py" in view 
    71.    return self.dispatch(request, *args, **kwargs) 
File "/home/oleshko/design/.virtualenvs/detsad-komplekt/local/lib/python2.7/site-packages/django/views/generic/base.py" in dispatch 
    89.   return handler(request, *args, **kwargs) 
File "/home/oleshko/design/detsad-komplekt/project/catalog/views.py" in get 
    109.   cat = parseCML() 
File "/home/oleshko/design/detsad-komplekt/project/catalog/tools.py" in parseCML 
    21.   name = name.decode('utf8', 'ignore') 
File "/home/oleshko/design/.virtualenvs/detsad-komplekt/lib/python2.7/encodings/utf_8.py" in decode 
    16.  return codecs.utf_8_decode(input, errors, True) 

Exception Type: UnicodeEncodeError at /catalog/commerce/ 
Exception Value: 'ascii' codec can't encode characters in position 0-6: ordinal not in range(128) 

這是我的函數:

def parseCML(): 
    with open('/home/oleshko/desktop/webdata/import.xml', 'r') as xml_file: 
     tree = etree.parse(xml_file) 
    root = tree.getroot() 
    elems = root[1][4] 
    res = [] 
    for prod in elems: 
     for cat in prod[4]: 
      c = Category.objects.filter(integration_id=cat.text)  
     name = prod[2].text 
     name = name.decode('utf8', 'ignore') 
     desc = prod[5].text 
     desc = desc.decode('utf8', 'ignore') 
     img = str(prod[6].text) 
     img = img.replace('import_files', '') 
     slug = pytils.translit.slugify(name) 
     # res.append([c, name, desc, img, '################']) 
     product = Product(name=name, slug=slug, category=c, description=desc, price=1) 
     new_product = product.save() 
     prod_img = ProductImage(product=new_product, src=img) 

    return True 

我瘦了4小時了努力解決這個問題。請給任何建議...

回答

0

的問題在這裏: C = Category.objects.filter(integration_id = cat.text)
我忘了,它返回類實例的queryset的替代,解碼出錯的地方,而放棄豁免。感謝用戶'catavaran'