gpt4 book ai didi

python - 如何处理重音字母、德语字母和其他字符?

转载 作者:太空宇宙 更新时间:2023-11-04 06:30:01 25 4
gpt4 key购买 nike

我的 python 脚本现在可以运行了,但是我遇到了一些小问题:

这是输出:

from BeautifulSoup import BeautifulSoup
import urllib

langCode={
"arabic":"ar", "bulgarian":"bg", "chinese":"zh-CN",
"croatian":"hr", "czech":"cs", "danish":"da", "dutch":"nl",
"english":"en", "finnish":"fi", "french":"fr", "german":"de",
"greek":"el", "hindi":"hi", "italian":"it", "japanese":"ja",
"korean":"ko", "norwegian":"no", "polish":"pl", "portugese":"pt",
"romanian":"ro", "russian":"ru", "spanish":"es", "swedish":"sv" }

def setUserAgent(userAgent):
urllib.FancyURLopener.version = userAgent
pass

def translate(text, fromLang, toLang):
setUserAgent("Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.0.1) Gecko/2008070400 SUSE/3.0.1-0.1 Firefox/3.0.1")
try:
postParameters = urllib.urlencode({"langpair":"%s|%s" %(langCode[fromLang.lower()],langCode[toLang.lower()]), "text":text,"ie":"UTF8", "oe":"UTF8"})
except KeyError, error:
print "Currently we do not support %s" %(error.args[0])
return

page = urllib.urlopen("http://translate.google.com/translate_t", postParameters)
content = page.read()
page.close()

htmlSource = BeautifulSoup(content)
translation = htmlSource.find('span', title=text )
return translation.renderContents()


print translate("Good morning to you friend!", "English", "German")
print translate("Good morning to you friend!", "English", "Italian")
print translate("Good morning to you friend!", "English", "Spanish")

Guten Morgen, du Freund!
Buongiorno a te amico!
Buenos días a ti amigo!

如何管理非基本英文字母?你会如何建议我解决这个问题?我在想一本字典来用另一个字符替换某些链,但我确信 Python 已经有了这样的东西。包括电池等等。 :P

谢谢。

最佳答案

不要解析 http://translate.google.com/translate_t,因为 Google 为此目的提供了 AJAX 服务。 ajax.googleapis.com返回的json数据中的translatedText已经是unicode字符串。

import urllib2
import urllib
import sys
import json

LANG={
"arabic":"ar", "bulgarian":"bg", "chinese":"zh-CN",
"croatian":"hr", "czech":"cs", "danish":"da", "dutch":"nl",
"english":"en", "finnish":"fi", "french":"fr", "german":"de",
"greek":"el", "hindi":"hi", "italian":"it", "japanese":"ja",
"korean":"ko", "norwegian":"no", "polish":"pl", "portugese":"pt",
"romanian":"ro", "russian":"ru", "spanish":"es", "swedish":"sv" }

def translate(text,lang1,lang2):
base_url='http://ajax.googleapis.com/ajax/services/language/translate?'
langpair='%s|%s'%(LANG.get(lang1.lower(),lang1),
LANG.get(lang2.lower(),lang2))
params=urllib.urlencode( (('v',1.0),
('q',text.encode('utf-8')),
('langpair',langpair),) )
url=base_url+params
content=urllib2.urlopen(url).read()
try: trans_dict=json.loads(content)
except AttributeError:
try: trans_dict=json.load(content)
except AttributeError: trans_dict=json.read(content)
return trans_dict['responseData']['translatedText']

print translate("Good morning to you friend!", "English", "German")
print translate("Good morning to you friend!", "English", "Italian")
print translate("Good morning to you friend!", "English", "Spanish")

产量

Guten Morgen, du Freund!
Buongiorno a te amico!
Buenos días a ti amigo!

关于python - 如何处理重音字母、德语字母和其他字符?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3981732/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com