python - how to add unicode literal to a variable?
Given a raw byte string, you can convert it to a unicode
object (Python 2.x) or a str
object (Python 3.x) by decoding it:
for name in ops.listdir(somedir.decode("utf-8")):
Use whatever encoding the byte string is encoded in instead of "utf-8"
. If you omit the encoding, Python's standard encoding will be used (ascii
in 2.x, utf-8
in 3.x).
See the Unicode HOWTO (3.x) for further information.
In case someone comes across this post like I did:
A little hack you can do is (u'%s' % somedir)
unicode(somedir)
e.g. use the builtin function
If the source of somedir
doesn't provide it as a Unicode string (isinstance(somedir, unicode)
is False) then you should decode it by providing an appropriate character encoding (it depends on where the bytes come from):
unicode_somedir = somedir.decode(encoding)