相关疑难解决方法(0)

在Python中将Unicode转换为ASCII而没有错误

我的代码只是抓取一个网页,然后将其转换为Unicode.

html = urllib.urlopen(link).read()
html.encode("utf8","ignore")
self.response.out.write(html)
Run Code Online (Sandbox Code Playgroud)

但我得到一个UnicodeDecodeError:


Traceback (most recent call last):
  File "/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/google/appengine/ext/webapp/__init__.py", line 507, in __call__
    handler.get(*groups)
  File "/Users/greg/clounce/main.py", line 55, in get
    html.encode("utf8","ignore")
UnicodeDecodeError: 'ascii' codec can't decode byte 0xa0 in position 2818: ordinal not in range(128)
Run Code Online (Sandbox Code Playgroud)

我认为这意味着HTML包含一些在某处错误形成的Unicode尝试.我可以删除导致问题的任何代码字节而不是出错吗?

python unicode ascii utf-8 character-encoding

170
推荐指数
9
解决办法
46万
查看次数

UnicodeEncodeError:'ascii'编解码器无法编码字符

上传具有非ASCII字符的文件时,我得到UnicodeEncodeError:

Exception Type: UnicodeEncodeError at /admin/studio/newsitem/add/
Exception Value: 'ascii' codec can't encode character u'\xf8' in position 78: ordinal not in range(128)
Run Code Online (Sandbox Code Playgroud)

查看完整堆栈跟踪.

我用MySQL和nginx以及FastCGI运行Django 1.2.

这是根据Django Trac数据库修复的问题,但我仍然遇到问题.欢迎任何有关如何修复的建议.

编辑:这是我的图像字段:

image = models.ImageField(_('image'), upload_to='uploads/images', max_length=100)
Run Code Online (Sandbox Code Playgroud)

django file-upload nginx django-admin django-uploads

36
推荐指数
5
解决办法
4万
查看次数

在Python中使用string.translate来音译西里尔语?

我得到UnicodeEncodeError: 'ascii' codec can't encode characters in position 0-51: ordinal not in range(128)异常尝试使用string.maketransPython.我对以下代码(gist)中的这种错误感到气馁:

# -*- coding: utf-8 -*-

import string

def translit1(string):
    """ This function works just fine """
    capital_letters = {
        u'?': u'A',
        u'?': u'B',
        u'?': u'V',
        u'?': u'G',
        u'?': u'D',
        u'?': u'E',
        u'?': u'E',
        u'?': u'Zh',
        u'?': u'Z',
        u'?': u'I',
        u'?': u'Y',
        u'?': u'K',
        u'?': u'L',
        u'?': u'M',
        u'?': u'N',
        u'?': u'O',
        u'?': u'P',
        u'?': u'R',
        u'?': u'S',
        u'?': u'T', …
Run Code Online (Sandbox Code Playgroud)

python transliteration

10
推荐指数
3
解决办法
2万
查看次数