2

I am a newbie and i don't know how to set my console to print urdu / arabic characters i am using Wing IDE when i run this code

print "طجکسعبکبطکسبطب"

i get this on my console

طجکسعبکبطکسبطب

M Talha Afzal
  • 231
  • 1
  • 5
  • 17
  • 1
    http://stackoverflow.com/questions/728891/correct-way-to-define-python-source-code-encoding – max Feb 07 '16 at 10:10
  • Is there any particular reason for your use of Python 2 language? I very much recommend that you'd use Python 3 which is very mature, for it hsa superior support for Unicode? – Antti Haapala -- Слава Україні Feb 07 '16 at 10:11
  • print u'طجکسعبکبطکسبطب' I'm on a computer without IDLE so I'm unable to test but I believe this may work. – Feek Feb 07 '16 at 10:12
  • 1
    Do note also that `"طجکسعبکبطکسبطب"` is a byte string, not an unicode string; and `u"طجکسعبکبطکسبطب"` is the way to go along with the `encoding` declaration. Nevertheless, you should **really** be using Python 3 unless you're maintaining some old code. – Antti Haapala -- Слава Україні Feb 07 '16 at 10:13
  • Also I guess that *Windows console* itself has limited support for Unicode anyway. – Antti Haapala -- Слава Україні Feb 07 '16 at 10:14
  • It's also worth adding onto @AnttiHaapala 's post that if you were using Python 3, print("طجکسعبکبطکسبطب") works perfectly. – Feek Feb 07 '16 at 10:16
  • when i use print u"جنس" i get this error File "C:\Python27\Lib\encodings\cp437.py", line 12, in encode return codecs.charmap_encode(input,errors,encoding_map) UnicodeEncodeError: 'charmap' codec can't encode characters in position 0-5: character maps to – M Talha Afzal Feb 07 '16 at 10:23

1 Answers1

0

You should encode your string arguments as unicode UTF-8 or later. Wrap the whole code in unicode, and/or mark individual string args as unicode (u'your text') too.

Additionally, you should make sure that unicode is enabled in your terminal/prompt window too.

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

arabic_words = u'لغت العربیه'

print arabic_words

enter image description here

Pouria
  • 1,081
  • 9
  • 24
  • I am getting error when i do this >>> #!/usr/bin/env python >>> # -*- coding: UTF-8 -*- >>> arabic = u'حسن' Unsupported characters in input – M Talha Afzal Feb 07 '16 at 10:55
  • Your declaration looks wrong here: `# -*- coding: UTF-8 -*-`. What operating system are you using? Did you make sure that your prompt window supports UTF-8? – Pouria Feb 07 '16 at 10:58
  • I am using windows 8.1 and how to check that my prompt window supports UTF-8? I declared correctly in my shell its the editor here – M Talha Afzal Feb 07 '16 at 11:07
  • Well then, there you have it! Good old Windows. Maybe try an alternative environment for this? Here are a few choices: `jupyter console`, `jupyter notebook`, `eclipse`, `pycharm`. These can replace Windows prompt with their own built-in prompts that are unicode compatible. Also, if you don't absolutely need to use Python 2, then please don't! Go for Python 3. I don't use Windows, and haven't for 10 years. So I cannot go into more details. But I'm sure it's problem with your prompt, since Python is passing everything in unicode now that you have implemented those settings. – Pouria Feb 07 '16 at 11:15
  • I am using Wing IDE and it treat this `# -*- coding: UTF-8 -*-` as a comment because of `#` – M Talha Afzal Feb 07 '16 at 11:26
  • Try PyCharm. I personally use it for my extensive projects. For simpler stuff, I use vim/emacs and Jupyter. Have a look! – Pouria Feb 07 '16 at 11:38
  • Also check the Debugger > I/O > Shell Encoding preference in Wing, which may be relevant. – Wingware Feb 08 '16 at 14:34
  • Although I just tried this here and get no output in the Python Shell in Wing. I think is due to the incomplete support for right-to-left languages in the underlying text editor widget (Scintilla). – Wingware Feb 08 '16 at 14:41