user299709
user299709

Reputation: 5412

Python: unable to convert unicode to string

I've tried everything but the unicode just doesn't go away.

col = "[u'$929.95']"
unicoded_item = to_unicode(col) # [u'test']

print type(unicoded_item) # <type 'unicode'>
if isinstance(unicoded_item, unicode):
    unicoded_item = unicoded_item.encode('utf8')
    print str(unicoded_item) # [u'test']

I expected the whole [u' and '] to disappear but it just doesn't seem to convert. So when I save this string to a text file, the text file will literally have all the unicode python character [u'test'] is literally written instead of test

Upvotes: 1

Views: 965

Answers (4)

LetzerWille
LetzerWille

Reputation: 5668

you string is not unicode. It is a regular string. You can get the dollar amount like this:

res = "[u'$929.95']".split("\'",)[1]
print(res)

$929.95

but if it were unicode with u'someletters, to remove u' ran str() on unicode str. .

Upvotes: 0

Burhan Khalid
Burhan Khalid

Reputation: 174662

You have a string that is the representation of a list object. The easiest way to get this thing sorted out, is to evaluate the string to get an object out:

>>> import ast
>>> col = "[u'$929.95']"
>>> col2 = ast.literal_eval(col)
>>> type(col)
<type 'str'>
>>> type(col2)
<type 'list'>
>>> col2[0]
u'$929.95'
>>> str(col2[0])
'$929.95'

Upvotes: 3

cg909
cg909

Reputation: 2556

The variable col probably contains a list with one unicode string element.

unicoded_item = to_unicode(col) then creates a unicode string with the representation of that list: u"[u'test']".

You then convert this unicode string to a string using unicoded_item.encode('utf8').

This gives you a (byte) string "[u'test']".

The solution is to access the element(s) in col instead of converting the whole col. If col always contains exactly one element you can simply replace the uses of col with col[0].

Upvotes: 2

Hill
Hill

Reputation: 71

It may not deal with the issue directly, but you could use the replace() function to swap the [u' for nothing.

Upvotes: 1

Related Questions