1

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. https://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?

Community
  • 1
  • 1
aeupinhere
  • 2,883
  • 6
  • 31
  • 39

1 Answers1

2

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()

Hyperboreus
  • 31,997
  • 9
  • 47
  • 87
  • Thanks a lot! Now what if my string is part arabic and part latin like this. 'وب (ar)' How do I get around that? – aeupinhere May 31 '11 at 23:25
  • But my input string is half latin and half arabic. – Hyperboreus May 31 '11 at 23:39
  • Very good point ;-) I am trying to translate some data using the bing translator and it can automatically detect the language. I am just having a hard time figuring out how to translate a string that is in two languages... – aeupinhere Jun 01 '11 at 01:03