Python翻译诗歌Ascii Troub

2024-09-30 14:15:42 发布

您现在位置:Python中文网/ 问答频道 /正文

我想找出一种方法来处理在标准Ascii图表中找不到的特殊字符。我正在做一些翻译诗来熟悉httplib和urllib模块。问题是当从一种语言翻译成另一种具有不同字母表的语言时,这意味着一些从英语到西班牙语/法语到英语的短语都可以工作,但前提是我必须提前明智地选择我的词汇,以避免任何冲突(达不到目的)。请原谅我说的那句奇怪的话,我真的没有办法用迷人的话。在

import httplib, urllib, json
connObj = httplib.HTTPConnection("api.mymemory.translated.net")
def simpleTrans(conn, text, ln1, ln2):
    paramDict = {'q': text,
                 'langpair':ln1+"|"+ln2}
    params = urllib.urlencode(paramDict)
    conn.request("GET","/get?"+params)
    res = connObj.getresponse()
    serializedText = res.read()
    responseDict = json.loads(serializedText)
    return responseDict['responseData']['translatedText']


a = simpleTrans(connObj, "man eats dogs for the sake of poetry police give him ten years in jail", 'en', 'fr')
b = simpleTrans(connObj, a, 'fr', 'es')
c = simpleTrans(connObj, b, 'es', 'no')
print (simpleTrans(connObj, c, 'no', 'en'))

这将产生如下预期的错误。在

^{pr2}$

如果有人能给我一些建议,我会非常感激的!在


Tags: text语言jsonresparamsurllibconnhttplib
1条回答
网友
1楼 · 发布于 2024-09-30 14:15:42

ASCII是一个有限的字符集,因为所有字符都需要用8位表示。我建议你看看Unicode。Unicode是一种标准格式,它不仅能够表示英语词汇。

您可以启动here。在

还可以看看函数decode()。在

st = 'ASCII character string.'
st.decode('utf-8')

相关问题 更多 >

    热门问题