python 脚本中的日语字符
Japanese characters in python script
我正在编写 python 脚本,将日语中的单词附加到常见类型 url。我的 IDE (spyder) 完美地显示了日语字符,浏览器和我的服务也是如此,但语言似乎有问题。例如,我的服务在我输入时给出了正确答案:
http://example.com/translate?lang=ja&word=こんりちは
然而在我的 IDE:
word = u'こんにちは'
In [29]: url = "http://example.com/translate?lang=ja&word=" + word
In [30]: word
Out[30]: u'\u3053\u3093\u306b\u3061\u306f'
In [31]: url
Out[31]: u'http://example.com/translate?lang=ja&word=\u3053\u3093\u306b\u3061\u306f'
我的服务无法识别 url。
如何解决?
浏览器url-为您编码查询字符串。您需要在 Python:
中手动完成
在Python2.x中,使用urllib.quote
or urllib.quote_plus
:
>>> import urllib
>>> word = u'こんにちは'
>>> url = "http://example.com/translate?lang=ja&word=" + urllib.quote(word.encode('utf-8'))
>>> url
'http://example.com/translate?lang=ja&word=%E3%81%93%E3%82%93%E3%81%AB%E3%81%A1%E3%81%AF'
在Python3.x中,使用urllib.parse.quote
or urllib.parse.quote_plus
:
>>> import urllib.parse
>>> word = u'こんにちは'
>>> url = "http://example.com/translate?lang=ja&word=" + urllib.parse.quote(word, encoding='utf-8')
>>> url
'http://example.com/translate?lang=ja&word=%E3%81%93%E3%82%93%E3%81%AB%E3%81%A1%E3%81%AF'
替代使用 urllib.urlencode
(Python 2.x)
>>> "http://example.com/translate?" + urllib.urlencode({'lang': 'ja', 'word': word.encode('utf-8')})
'http://example.com/translate?lang=ja&word=%E3%81%93%E3%82%93%E3%81%AB%E3%81%A1%E3%81%AF'
使用 urllib.parse.urlencode
(Python 3.x)
>>> "http://example.com/translate?" + urllib.parse.urlencode({'lang': 'ja', 'word': word}, encoding='utf-8')
'http://example.com/translate?lang=ja&word=%E3%81%93%E3%82%93%E3%81%AB%E3%81%A1%E3%81%AF'
我正在编写 python 脚本,将日语中的单词附加到常见类型 url。我的 IDE (spyder) 完美地显示了日语字符,浏览器和我的服务也是如此,但语言似乎有问题。例如,我的服务在我输入时给出了正确答案: http://example.com/translate?lang=ja&word=こんりちは
然而在我的 IDE:
word = u'こんにちは'
In [29]: url = "http://example.com/translate?lang=ja&word=" + word
In [30]: word
Out[30]: u'\u3053\u3093\u306b\u3061\u306f'
In [31]: url
Out[31]: u'http://example.com/translate?lang=ja&word=\u3053\u3093\u306b\u3061\u306f'
我的服务无法识别 url。 如何解决?
浏览器url-为您编码查询字符串。您需要在 Python:
中手动完成在Python2.x中,使用urllib.quote
or urllib.quote_plus
:
>>> import urllib
>>> word = u'こんにちは'
>>> url = "http://example.com/translate?lang=ja&word=" + urllib.quote(word.encode('utf-8'))
>>> url
'http://example.com/translate?lang=ja&word=%E3%81%93%E3%82%93%E3%81%AB%E3%81%A1%E3%81%AF'
在Python3.x中,使用urllib.parse.quote
or urllib.parse.quote_plus
:
>>> import urllib.parse
>>> word = u'こんにちは'
>>> url = "http://example.com/translate?lang=ja&word=" + urllib.parse.quote(word, encoding='utf-8')
>>> url
'http://example.com/translate?lang=ja&word=%E3%81%93%E3%82%93%E3%81%AB%E3%81%A1%E3%81%AF'
替代使用 urllib.urlencode
(Python 2.x)
>>> "http://example.com/translate?" + urllib.urlencode({'lang': 'ja', 'word': word.encode('utf-8')})
'http://example.com/translate?lang=ja&word=%E3%81%93%E3%82%93%E3%81%AB%E3%81%A1%E3%81%AF'
使用 urllib.parse.urlencode
(Python 3.x)
>>> "http://example.com/translate?" + urllib.parse.urlencode({'lang': 'ja', 'word': word}, encoding='utf-8')
'http://example.com/translate?lang=ja&word=%E3%81%93%E3%82%93%E3%81%AB%E3%81%A1%E3%81%AF'