9 Replies - 1726 Views - Last Post: 02 May 2011 - 01:20 PM Rate Topic: -----

#1 wisam abbasi  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 72
  • Joined: 12-December 09

strange characters in python

Posted 02 May 2011 - 10:49 AM

I am processing Arabic files using python
It works normally, but when I use the TK GUI to display the text it gives strings as follows:
نورمبيرغ - من إينيس باير فابر-كاستل 
يقول أنطون فولفغانغ غراف فون فابر-كاستلᄀ رئيس المؤسسة الالمانية لتصنيع الادوات الكتابية التي تحمل الاسم ذاتهᄀ إنه بصرف النظر عن الانترنت ورسائل الهاتف المحمول وأجهزة الكمبيوتر التي يمكن حملها في اليد الواحدɡ فإن أقلام الرصاص المتواضعة ما يزال أمامها مستقبل.


So, What's the problem?

Is This A Good Question/Topic? 0
  • +

Replies To: strange characters in python

#2 Motoma  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 450
  • View blog
  • Posts: 795
  • Joined: 08-June 10

Re: strange characters in python

Posted 02 May 2011 - 10:54 AM

More than likely it is a font issue. Try setting the font manually to something you know can properly represent the character set you are using.

Disclaimer: I have no experience with programming in non-English languages.
Was This Post Helpful? 0
  • +
  • -

#3 wisam abbasi  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 72
  • Joined: 12-December 09

Re: strange characters in python

Posted 02 May 2011 - 11:03 AM

View PostMotoma, on 02 May 2011 - 11:54 AM, said:

More than likely it is a font issue. Try setting the font manually to something you know can properly represent the character set you are using.

Disclaimer: I have no experience with programming in non-English languages.

I've tried so, and it didn't work :surrender:
Was This Post Helpful? 0
  • +
  • -

#4 mi14chal  Icon User is offline

  • D.I.C Head

Reputation: 81
  • View blog
  • Posts: 198
  • Joined: 11-December 10

Re: strange characters in python

Posted 02 May 2011 - 11:34 AM

Well you'll try to it:
text = u"Arabic text"

And after pass variable text to function will show this text.
Was This Post Helpful? 0
  • +
  • -

#5 wisam abbasi  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 72
  • Joined: 12-December 09

Re: strange characters in python

Posted 02 May 2011 - 12:08 PM

View Postmi14chal, on 02 May 2011 - 12:34 PM, said:

Well you'll try to it:
text = u"Arabic text"

And after pass variable text to function will show this text.

Ok, but what if you have somthing like this:
input=open('file.txt','r')
text=input.read()
input.close


and now you want to display the text

text=r(text)


will not work
Was This Post Helpful? 0
  • +
  • -

#6 mi14chal  Icon User is offline

  • D.I.C Head

Reputation: 81
  • View blog
  • Posts: 198
  • Joined: 11-December 10

Re: strange characters in python

Posted 02 May 2011 - 12:22 PM

Read the paragraph Reading UTF-8 Files on this website: http://www.evanjones...ython-utf8.html
Was This Post Helpful? 0
  • +
  • -

#7 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon

Reputation: 4887
  • View blog
  • Posts: 11,282
  • Joined: 16-October 07

Re: strange characters in python

Posted 02 May 2011 - 12:32 PM

This worked:
s = u'\u0645\u0631\u062d\u0628\u0627 \u0627\u0644\u0639\u0627\u0644\u0645'

from Tkinter import *

root = Tk()

w = Label(root, text=s)
w.pack()

root.mainloop()



This didn't work:
open('dump', 'w').write(s)
s2 = open('dump', 'r').read()



This did:
open('dump', 'w').write(s.encode('UTF-8'))
s2 = codecs.open('dump', "r", "utf-8" ).read()



Hope this helps.
Was This Post Helpful? 0
  • +
  • -

#8 wisam abbasi  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 72
  • Joined: 12-December 09

Re: strange characters in python

Posted 02 May 2011 - 12:46 PM

View Postmi14chal, on 02 May 2011 - 01:22 PM, said:

Read the paragraph Reading UTF-8 Files on this website: http://www.evanjones...ython-utf8.html

I've tried the examples mentioned to Arabic strings, and here is the output:
s = "وسااام"
u = unicode( s, "utf-8" )
backToBytes = u.encode( "utf-8" )
print backToBytes



and here is the error:
Traceback (most recent call last):
  File "C:\Users\one.omary\Desktop\AJURL.pyw", line 2, in <module>
    u = unicode( s, "utf-8" )
UnicodeDecodeError: 'utf8' codec can't decode byte 0xe6 in position 0: invalid continuation byte
>>> 


Was This Post Helpful? 0
  • +
  • -

#9 wisam abbasi  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 72
  • Joined: 12-December 09

Re: strange characters in python

Posted 02 May 2011 - 12:56 PM

View Postbaavgai, on 02 May 2011 - 01:32 PM, said:

This worked:
s = u'\u0645\u0631\u062d\u0628\u0627 \u0627\u0644\u0639\u0627\u0644\u0645'

from Tkinter import *

root = Tk()

w = Label(root, text=s)
w.pack()

root.mainloop()



This didn't work:
open('dump', 'w').write(s)
s2 = open('dump', 'r').read()



This did:
open('dump', 'w').write(s.encode('UTF-8'))
s2 = codecs.open('dump', "r", "utf-8" ).read()



Hope this helps.

I am confused I don't know if the problem in the coding or in me. It gives this error:
Exception in Tkinter callback
Traceback (most recent call last):
  File "C:\Python27\lib\lib-tk\Tkinter.py", line 1410, in __call__
    return self.func(*args)
  File "C:\Users\one.omary\Desktop\GP\Atext.pyw", line 75, in sos
    open('s.txt','w').write(s.encode('UTF-8'))
UnicodeDecodeError: 'ascii' codec can't decode byte 0xe4 in position 80: ordinal not in range(128)
>>> 


Was This Post Helpful? 0
  • +
  • -

#10 wisam abbasi  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 72
  • Joined: 12-December 09

Re: strange characters in python

Posted 02 May 2011 - 01:20 PM

:hang:
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1