python - how to add unicode literal to a variable?

Viewed 20659

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)

?

5 Answers

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.

Related