I have a python list that looks like that:
list = [u'a', u'b', u'c']
Now I want to encode it in UTF-8. Therefore I though I should use:
list = list[0].encode("utf-8")
But print list gives only
a
meaning the first element of the list. Not even a list anymore. What am I doing wrong?
UTF-8 is a byte oriented encoding. The encoding specifies that each character is represented by a specific sequence of one or more bytes.
The encode() method encodes the string, using the specified encoding. If no encoding is specified, UTF-8 will be used.
To encode string array values, use the numpy. char. encode() method in Python Numpy. The arr is the input array to be encoded.
>>> items = [u'a', u'b', u'c']
>>> [x.encode('utf-8') for x in items]
['a', 'b', 'c']
list[0]
is the first element, not a list. you are reassigning your list
var to a new value, the utf-8 encoding of the first element.
Also, don't name your variables list
, as it masks the list()
function.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With