0

我的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 :

谁能告诉我为什么会显示此错误?如何删除此错误

请帮忙。

4

2 回答 2

0

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

k = u"{0}".format( text_only ).count(str1)
于 2012-07-04T15:33:30.777 回答
0

您正在混合 Unicode 字符串和字节串。str1 = request.POST['word']可能是一个 Unicode 字符串并且text_only是一个字节串。Python 无法将后者转换为 Unicode。您可以使用codecs.open()指定文件的字符编码。请参阅实用 Unicode每个软件开发人员绝对、肯定必须了解 Unicode 和字符集的绝对最小值(没有借口!)

于 2012-07-04T15:37:38.390 回答