Python Forum
typing farsi (persian) in python - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: GUI (https://python-forum.io/forum-10.html)
+--- Thread: typing farsi (persian) in python (/thread-2115.html)

Pages: 1 2 3 4


typing farsi (persian) in python - gray - Feb-20-2017

how can i type farsi text in python? i don't have any source and i don't know how i start?
i want to write farsi text in SQL table?
please help
i need a suitable source..i don't get any usable content from my search


RE: typing farsi (persian) in python - wavic - Feb-20-2017

In Python 3 all strings are Unicode characters.

You can use str.encode() method too - "สวัสดีชาวโลก!".encode(encoding="UTF-8").


RE: typing farsi (persian) in python - snippsat - Feb-20-2017

As mention bye Wavic,Python 3.x did make big changes to Unicode.
When you inside Python 3.x are strings Unicode characters.
All Unicode work like regular string,because that what they are.
Eg:
>>> s = "สวัสดีชาวโลก!"
>>> s
'สวัสดีชาวโลก!'
>>> type(s)
<class 'str'>
>>> s = s + 'a'
>>> s
'สวัสดีชาวโลก!a'
>>> s.count('ด')
1
>>> s.count('a')
1
Out and in of Python we have to use a encoding.
>>> with open('uni.txt', 'w', encoding='utf-8') as f:
...     f.write(s)
...     
14
>>> with open('uni.txt', encoding='utf-8') as f:
...     print(f.read())
... 
สวัสดีชาวโลก!a
Quote:i want to write farsi text in SQL table?
The advice is to keep utf-8 all the way in and out(also with database).


RE: typing farsi (persian) in python - gray - Feb-20-2017

i don't understand your code
this is my code...but i don get a suitable output
please helpppp
thank you
from tkinter import*
barname=Tk()

barname.geometry('200x120')
barname.title('عنوان')


tyLabel = Label(barname,text="راوند")


tyLabel.pack()
mainloop()



RE: typing farsi (persian) in python - snippsat - Feb-20-2017

Quote:i don't understand your code
It's just copy of farsi text from waic post,and doing test with it in interactive  shell.
You can do the same,you know that Python has a interactive shell?

Use code tag.
Here a picture of my tkinter window,running Python 3.6


RE: typing farsi (persian) in python - merlem - Feb-20-2017

Maybe already the first post has the answer.
Set into your code:
barname.title('عنوان'.encode(encoding="UTF-8"))
tyLabel = Label(barname,text="راوند".encode(encoding="UTF-8"))
Could this work?


RE: typing farsi (persian) in python - snippsat - Feb-20-2017

(Feb-20-2017, 02:15 PM)merlem Wrote: Could this work?
No,the point is that you do not encode when you are running code inside Python 3(only in/out).
In Python 3 are all strings sequences of Unicode character.
This give you bytes:
>>> text = "راوند".encode(encoding="UTF-8")
>>> text
b'\xd8\xb1\xd8\xa7\xd9\x88\xd9\x86\xd8\xaf'

>>> # Correct
>>> text = "راوند"
>>> text
'راوند'
Look at my picture,i just copy code from @gray post and run it.
It has correct correct text in GUI window.


RE: typing farsi (persian) in python - merlem - Feb-20-2017

Okay, that's no good then, sorry.

However, If I run the code gray had given, I got a different result: the text inside the window seems to be okay, but the barname is replaced by small boxes.


RE: typing farsi (persian) in python - gray - Feb-21-2017

(Feb-20-2017, 02:15 PM)merlem Wrote: Maybe already the first post has the answer.
Set into your code:
barname.title('عنوان'.encode(encoding="UTF-8"))
tyLabel = Label(barname,text="راوند".encode(encoding="UTF-8"))
Could this work?

it doesn't work...how shoud i do?
please help............


RE: typing farsi (persian) in python - wavic - Feb-21-2017

In your script/program first line is something like this: #!/usr/bin/env python3. Be sure that it's python3 not just python.