0

I have following string

test="ن گ ب ن د ی ک ر و ا ن "

what I want is that I want to access each character and save it in some variables for future access but when I looped over them I got weird output.Actually I am not aware of encoding schemes that much.

for i in test:
    print(i)

above code gave me some weird characters what I want is the original script characters?

Naseer
  • 4,041
  • 9
  • 36
  • 72
  • Does the console you print to support these characters? Here on my Ubunut it works fine. How is your Python file encoded? –  Jul 19 '16 at 15:01
  • Do you use Python 3 or Python 2? –  Jul 19 '16 at 15:03

2 Answers2

3

Either define test as a unicode string, or use the decode method:

test="ن گ ب ن د ی ک ر و ا ن"
for i in test.decode('utf8'):
    print(i)
    # print unicode value
    print(repr(i))

test=u"ن گ ب ن د ی ک ر و ا ن"
for i in test:
    print(i)
    # print unicode value
    print(repr(i))

Obviously my answer concerns Python 2.7.x.

Frodon
  • 3,684
  • 1
  • 16
  • 33
  • 1
    @FullName yes but then its code would not print weird characters – Frodon Jul 19 '16 at 15:06
  • The code from the question copied and pasted into an interactive Python 3 session on a terminal that supports Unicode works without any problem. So the question is incomplete. –  Jul 19 '16 at 15:07
  • @Frodon Your first code snipped used in Python 3 throws `AttributeError: 'str' object has no attribute 'decode'`. –  Jul 19 '16 at 15:09
  • @FullName yes, that's why it is written `Obviously my answer concerns Python 2.x` – Frodon Jul 19 '16 at 15:10
  • `print(i)` is not Python 2. The question is unclear. –  Jul 19 '16 at 15:11
  • @Frodon Please also tell me how to get the unicode of the corresponding character? – Naseer Jul 19 '16 at 15:14
  • @NaseerAhmed change `print(i)` by `print(repr(i))`. I updated my answer. – Frodon Jul 19 '16 at 15:19
0

For Python 2.x try this:

test=u"ن گ ب ن د ی ک ر و ا ن "
for i in test:
    print(i)

Appending u makes it a unicode object.

shiva
  • 2,535
  • 2
  • 18
  • 32