开发者

Python Unicode %HH-escaping

开发者 https://www.devze.com 2023-03-10 00:54 出处:网络
All, I am working on an application that has a mixture of standard latin-1 characters and other languages. I would like to convert everything to the same \"format\" so that the Bing translator can un

All,

I am working on an application that has a mixture of standard latin-1 characters and other languages. I would like to convert everything to the same "format" so that the Bing translator can understand it. There are two articles that I've looked at.

  1. http开发者_开发问答s://dev.laptop.org/ticket/2327 I actually want to do the reverse of this and convert this http://ar.wikipedia.org/wiki/حاسوب to this http://ar.wikipedia.org/wiki/%D8%AD%D8%A7%D8%B3%D9%88%D8%A8

  2. Similar article but not quite what I was looking for. How to unquote a urlencoded unicode string in python?

My method is quite simple...

    for w in wiki:
        q = (w[0])
        u = unicode(q, 'utf=8', errors='ignore')
        h = ''
        for c in u:
            h += do something amazing

        doTranslate(h)

Can anyone help to shed some light on what I am missing here?


urllib might help. At least this snippet works:

#! /usr/bin/env python
# -*- coding: utf-8 -*-

import urllib

w = 'ar.wikipedia.org/wiki/حاسوب'
print urllib.quote (w)

Output is

ar.wikipedia.org/wiki/%D8%AD%D8%A7%D8%B3%D9%88%D8%A8

Depending which encoding your input strings have you might need to call encode ('utf-8') prior to passing it to quote()

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号