I've seen some examples like this:
for name in os.listdir(u'somedir') :
my problem is that I'm getting the somedir as a variable, so how can I append the 'u' literal?
something like
for name in ops.listdir(u+somedir)
?
I've seen some examples like this:
for name in os.listdir(u'somedir') :
my problem is that I'm getting the somedir as a variable, so how can I append the 'u' literal?
something like
for name in ops.listdir(u+somedir)
?
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.
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)
In case someone comes across this post like I did:
A little hack you can do is (u'%s' % somedir)
Simple solution is to use unicode function as follows:
x = unicode('1.2.3.4')
print x
u'1.2.3.4'
type(x)
type 'unicode'
It shows type as unicode now.