2

I am working on a project where Arabic language is used and I am using pyqt for the same. There is a text box in which I enters in Arabic then take that Arabic in a variable and read it using python. I have tried using UTF-8 as well but didn't worked.

For example I am entering

بسم الله الرحمن الرحيم

in the text box but when I read content of that box, I get the variable something as:

???? ????? ????

If I do not use the Unicode, then it gives me error as

ASCII code cannot encode characters.

How get same print variable in Arabic as entered in box ?


code is :

    item=dir(self.listWidget.selectedItems())
    item=self.listWidget.currentItem()
    content=self.textEdit.toPlainText()
    content = unicode(content, "utf-8")
    FROMADDR = ""
    LOGIN = FROMADDR
    PASSWORD = ""
    TOADDRS = str(item.text())
    SUBJECT = "Invitation"


    msg = ("From: %s\r\nTo: %s\r\nSubject: %s\r\n\r\n"
    % (FROMADDR, ", ".join(TOADDRS), SUBJECT) )
    msg += ((u"%s")%content)#"some text\r\n" 
    server = smtplib.SMTP('smtp.gmail.com', 587)
    server.set_debuglevel(1)
    server.ehlo()
    server.starttls()
    server.login(LOGIN, PASSWORD)
    server.sendmail(FROMADDR, TOADDRS, msg)
    server.quit()
ekhumoro
  • 115,249
  • 20
  • 229
  • 336
sam
  • 18,509
  • 24
  • 83
  • 116
  • which version of python are you using ? there are a lot of difference in unicode character handling between python 2 and python 3. – Adrien Plisson Nov 26 '11 at 14:30
  • @thomas : its simple to test as u can take command prompt and try adding/pasting a arabic word as string, it will show u as ?????? – sam Nov 26 '11 at 14:34
  • @sam: That depends on what operating system you're using. In Linux, I can get arabic characters at a terminal no problem. – Thomas K Nov 26 '11 at 14:36
  • @sam: I thought it might be Windows. But PyQt should still be able to work with unicode. – Thomas K Nov 26 '11 at 14:41
  • @thomas : I am not able to do. I have searched and tried but not get answers. so posted it here. :( – sam Nov 26 '11 at 14:43
  • @sam: You might want to show some code. – Thomas K Nov 26 '11 at 14:44
  • It's almost certainly getting the correct value, it's just that the Windows terminal can't display it. If you display it in another Qt widget, you should see the Arabic. – Thomas K Nov 26 '11 at 14:52
  • @thomas : i am displaying that in email. I will show the code – sam Nov 26 '11 at 15:03
  • @sam: There's an edit link beneath your question which you can use to add your code. Don't post edits as answers. – BoltClock Nov 26 '11 at 16:17
  • This is probably an issue with sending the email. I've not done anything with that. You might want to look at the `email` package: http://docs.python.org/library/email.html#module-email – Thomas K Nov 26 '11 at 16:20

2 Answers2

1

Use the appropriate email package classes to ensure the encoding is done properly:

from email.mime.text import MIMEText
from email.header import Header
...

LOGIN = ''
PASSWORD = ''
SUBJECT = u'Invitation'
FROMADDR = u''
TOADDRS = unicode(self.listWidget.currentItem().text())
CONTENT = unicode(self.textEdit.toPlainText())

encoding = 'utf-8'

msg = MIMEText(CONTENT, 'plain', encoding)
msg['Subject'] = Header(SUBJECT, encoding)
msg['From'] = Header(FROMADDR, encoding)
msg['To'] = Header(TOADDRS, encoding)

server = smtplib.SMTP('smtp.gmail.com', 587)
server.set_debuglevel(1)
server.ehlo()
server.starttls()
server.login(LOGIN, PASSWORD)
server.sendmail(FROMADDR, [TOADDRS], msg.as_string())
server.quit()
ekhumoro
  • 115,249
  • 20
  • 229
  • 336
1

This is a problems with Windows command prompt. It is not able to reliably display Unicode characters. You need to use an IDE such as IDLE to display and verify your output.

There is more information in this question Outputting unicode characters in windows terminal and in some of the links that they give.

Community
  • 1
  • 1
Michael Dillon
  • 31,973
  • 6
  • 70
  • 106