20

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)

?

hippietrail
  • 15,848
  • 18
  • 99
  • 158
user1251654
  • 1,097
  • 4
  • 13
  • 21

5 Answers5

7

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.

Sven Marnach
  • 574,206
  • 118
  • 941
  • 841
5

unicode(somedir)

e.g. use the builtin function

David Lam
  • 4,689
  • 3
  • 23
  • 34
  • Note that in general you will need to provide an encoding, and that the `encode()` and `decode()` methods are the preferred way to ocnvert between raw byte strings and Unicode strings. – Sven Marnach Aug 09 '12 at 17:57
5

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)
jfs
  • 399,953
  • 195
  • 994
  • 1,670
3

In case someone comes across this post like I did:

A little hack you can do is (u'%s' % somedir)

darisoy
  • 73
  • 6
1

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.

Azher Rana
  • 11
  • 2