Devnagari Unicode Conversion Issues

D

darpan6aya

How can i convert text of the following type

नेपाली

into devnagari unicode in Python 2.7?
 
M

MRAB

How can i convert text of the following type

नेपाली

into devnagari unicode in Python 2.7?
Is that a bytestring? In other words, is its type 'str'?

If so, you need to decode it. That particular string is UTF-8:
नेपाली
 
D

darpan6aya

That worked out. I was trying to encode it the entire time.
Now I realise how silly I am.

Thanks MRAB. Once Again. :D
 
D

Dave Angel

That worked out. I was trying to encode it the entire time.
Now I realise how silly I am.

Thanks MRAB. Once Again. :D

you're not silly, it's a complex question. MRAB is good at guessing
which part is messing you up.

However, when you're writing a real Python program with a real text
editor, and when you're not using a newsgroup in between to mangle or
unmangle things, you have a few things to match up to get it right.


The file is just a bunch of bytes. Those bytes are being inserted in
there by your editor, and interpreted by the compiler. So if you have a
non-ASCII character on your keyboard and you hit it, the editor will
decode it (from Unicode to byte(s)) and put it in the file. If you tell
the editor to use utf-8, then you also want to tell the compiler to
decode it using utf-8.

The most polite way to do that looks something like:
# -*- coding: <encoding-name> -*-
# -*- coding: <utf-8> -*-

http://docs.python.org/release/2.7.5/reference/lexical_analysis.html#encoding-declarations

Once you've got that straight, you don't need to explicitly decode byte
strings. You can just use
u"This is my string"

with whatever characters you need. As long as the declarations match,
this should "just work." If the data comes from a byte string other
than a literal string, you might need the more verbose form.

Your original message was sent in Western (ISO 8859-1), and MRAB's
response was in utf-8, and my mail program decoded the string the same
way. However, I don't know anything about Devnagari, so I can't say if
it looked reasonable here.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,135
Messages
2,570,783
Members
47,340
Latest member
orhankaya

Latest Threads

Top