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

Unicode文字列とバイト文字列を混在させています。str1 = request.POST['word']おそらくUnicode文字列でtext_onlyあり、バイト文字列です。Pythonは後者をUnicodeに変換できません。codecs.open()ファイルの文字エンコードを指定するために使用できます。実用的なUnicode絶対最小すべてのソフトウェア開発者は絶対に、積極的にUnicodeと文字セットについて知っている必要があります(言い訳はありません!)を参照してください。

于 2012-07-04T15:37:38.390 に答える
0

おそらくstr1はユニコードですが、text_onlyはそうではありません(34行目)。次は万能薬ではありませんが、これで問題が解決するなら、私は正しいです。

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