运行应用程序(django)上服务器上显示的unicode错误

时间:2012-07-04 15:17:15

标签: python django linux unicode ubuntu-10.04

我的views.py代码:

#!/usr/bin/python 

from django.template import loader, RequestContext
from django.http import HttpResponse
#from skey import find_root_tags, count, sorting_list
from search.models import Keywords
from django.shortcuts import render_to_response as rr

def front_page(request):

    if request.method == 'POST' :
        from skey import find_root_tags, count, sorting_list
        str1 = request.POST['word'] 
        fo = open("/home/pooja/Desktop/xml.txt","r")

        for i in range(count.__len__()):

            file = fo.readline()
            file = file.rstrip('\n')
            find_root_tags(file,str1,i) 

            list.append((file,count[i]))

        sorting_list(list)

        for name, count1 in list:
            s = Keywords(file_name=name,frequency_count=count1)
            s.save()

        fo.close()

        list1 = Keywords.objects.all()
        t = loader.get_template('search/results.html')
        c = RequestContext({'list1':list1,
        })

        return HttpResponse(t.render(c))

    else :  
        str1 = ''
        list = []
        template = loader.get_template('search/front_page.html')
        c = RequestContext(request)
        response = template.render(c)
        return HttpResponse(response)

skey.py还有另一个来自find_root_tags()的函数:

        def find_text(file,str1,i):

            str1 = str1.lower()
            exp = re.compile(r'<.*?>')
            with open(file) as f:
            lines = ''.join(line for line in f.readlines())
            text_only = exp.sub('',lines).strip()

            text_only = text_only.lower()
            k = text_only.count(str1)  #**line 34**
            count[i] = count[i]+k

当我在服务器上运行我的应用时,它给了我这个错误:

UnicodeDecodeError at /search/
'ascii' codec can't decode byte 0xef in position 0: ordinal not in range(128)
Request Method: POST
Request URL:    http://127.0.0.1:8000/search/
Django Version: 1.4
Exception Type: UnicodeDecodeError
Exception Value:    
'ascii' codec can't decode byte 0xef in position 0: ordinal not in range(128)
Exception Location: /home/pooja/Desktop/mysite/search/skey.py in find_text, line 34
Python Executable:  /usr/bin/python
Python Version: 2.6.5
Python Path:     ['/home/pooja/Desktop/mysite',
                     '/usr/lib/python2.6',
                     '/usr/lib/python2.6/plat-linux2',
                     '/usr/lib/python2.6/lib-tk',
                     '/usr/lib/python2.6/lib-old',
                     '/usr/lib/python2.6/lib-dynload',
                     '/usr/lib/python2.6/dist-packages',
                     '/usr/lib/python2.6/dist-packages/PIL',
                     '/usr/lib/python2.6/dist-packages/gst-0.10',
                     '/usr/lib/pymodules/python2.6',
                     '/usr/lib/python2.6/dist-packages/gtk-2.0',
                     '/usr/lib/pymodules/python2.6/gtk-2.0',
                     '/usr/local/lib/python2.6/dist-packages'] error :

任何人都可以告诉我为什么会出现此错误?如何删除此错误

请帮忙。

2 个答案:

答案 0 :(得分:0)

您的 str1 可能是unicode,但text_only不是(第34行)。下一个不是灵丹妙药,但如果这能解决你的问题,那么我是对的。

k = u"{0}".format( text_only ).count(str1)

答案 1 :(得分:0)

您正在混合使用Unicode字符串和字节串。 str1 = request.POST['word']可能是Unicode字符串,text_only是字节字符串。 Python无法将后者转换为Unicode。您可以使用codecs.open()指定文件的字符编码。请参阅Pragmatic UnicodeThe Absolute Minimum Every Software Developer Absolutely, Positively Must Know About Unicode and Character Sets (No Excuses!)

相关问题