0

出现错误信息,例如:

Internal Server Error: /Translator/
Traceback (most recent call last):
  File "D:\Python27\lib\site-packages\django\core\handlers\base.py", line 115, in get_response
    response = callback(request, *callback_args, **callback_kwargs)
  File "D:\Project\Reservation\Translator\views.py", line 72, in getParams
    content = request.POST['content'].decode('utf-8').encode('utf-8')
  File "D:\Python27\lib\encodings\utf_8.py", line 16, in decode
    return codecs.utf_8_decode(input, errors, True)
UnicodeEncodeError: 'ascii' codec can't encode characters in position 0-1: ordinal not in range(128)
[05/Oct/2013 23:32:51] "POST /Translator/ HTTP/1.1" 500 65244
Internal Server Error: /Translator/
Traceback (most recent call last):
  File "D:\Python27\lib\site-packages\django\core\handlers\base.py", line 115, in get_response
    response = callback(request, *callback_args, **callback_kwargs)
  File "D:\Project\Reservation\Translator\views.py", line 72, in getParams
    content = request.POST['content'].decode('utf-8').encode('utf-8')
  File "D:\Python27\lib\encodings\utf_8.py", line 16, in decode
    return codecs.utf_8_decode(input, errors, True)
UnicodeEncodeError: 'ascii' codec can't encode character u'\xe1' in position 2: ordinal not in range(128)

我的代码如下:

def get_access_token():
    post_data = urllib.urlencode({'client_id':client_id,'client_secret':client_secret, 'scope':ACCESS_TOKEN_SCOPE, 'grant_type':ACCESS_TOKEN_GRANT_TYPE })
    token_data = json.loads(requests.post(ACCESS_TOKEN_URL,data=post_data).content)
    access_token = token_data["access_token"]

    return access_token

def detect(access_token,detect_text):

    headers = {'Authorization': 'bearer'+ ' ' + access_token}

    detect_url_all = DETECT_URL + "?" + urllib.urlencode({'text':detect_text})
    detect_language = requests.get(detect_url_all,headers=headers).content[3:]

    return detect_language


def Translator(text,orignal,access_token):

    headers = {'Authorization': 'bearer'+ ' ' + access_token}

    translation_ars = {
            'text': text,
            'to': 'zh',
            'from': orignal
        }

    transate_url_all = TRANSLATE_URL + "?" + urllib.urlencode(translation_ars)
    result = requests.get(transate_url_all,headers=headers).content
    return result

def getParams(request):
    if request.method == 'POST':
        form = Junk(request.POST)
        if form.is_valid():
            content = request.POST['content'].decode('utf-8').encode('utf-8')
            country = detect(get_access_token(),content)
            result = Translator(content,country,get_access_token())
            return render_to_response('Translator/translate.html',{'result':result})
    else:
        form = Junk()
    return render_to_response('Translator/index.html',{'form': form})

首先,我想先检测文本的语言。我的程序无法知道文本的编码是什么,所以我无法进行解码或编码。

4

2 回答 2

0

错误消息表明request.POST['content']已经是 Unicode。.decode('utf-8')期望对象是字节字符串,因此使用隐式.encode('ascii')将 Unicode 字符串转换为字节字符串。因为它已经是一个 Unicode 字符串,而且您似乎想要一个 UTF-8 字节字符串,所以您只需要:

content = request.POST['content'].encode('utf-8')

request必须已getParams作为 Unicode 字符串传递给,但您的代码的那部分不存在。

如果您还有其他问题,此示例可能会有所帮助:https ://gist.github.com/dpapathanasiou/2790853

于 2013-10-06T17:35:30.850 回答
0

我认为问题很简单,你使用了错误的方法。使用result.encode(而不是result.decode)方法将其编码为utf-8编码。

对不起我迟到的答案。

于 2013-10-05T07:25:25.713 回答