【问题标题】:unicode error displayed on the server on running app (django)运行应用程序(django)时服务器上显示的 unicode 错误
【发布时间】:2012-07-04 15:17:14
【问题描述】:

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

谁能告诉我为什么会出现这个错误?我怎样才能消除这个错误

请帮忙。

【问题讨论】:

  • 谁能提供我的代码.......请帮忙,我没有时间阅读一些东西很紧急
  • 请指定在哪里调用 find_text
  • 查看我的find_text版本
  • 我改变了答案。请测试一下。
  • 作为一个快速破解你可以text_only = text_only.decode(character_encoding),但它只是将问题转移到其他地方。正确的解决方法是在内部各处使用 Unicode 字符串。

标签: python django linux unicode ubuntu-10.04


【解决方案1】:

您正在混合使用 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!)

【讨论】:

    【解决方案2】:

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

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

    【讨论】:

    • UTF-16 引用具有误导性。 Unicode 字符串是 Unicode 代码点序列(忽略代理对)。 Python 中 Unicode 字符串的内部表示取决于编译器选项、python 版本
    • @J.F.Sebastian 关于字符串文字,我的建议有误吗?
    • 你需要指定一个字符编码。否则会发生同样的错误
    • @J.F.Sebastian 如果 str1 不是 unicode 对象(UTF-16),你是对的
    • 不,试试:unicode('байты').count(u'unistr')。它应该产生错误
    猜你喜欢
    • 2021-05-03
    • 1970-01-01
    • 1970-01-01
    • 2022-10-13
    • 1970-01-01
    • 2017-06-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多