0

I need to join something like this:

["Círculo", 23]
["Triángulo, 25, 19, "dos"]

I have seen this post -> Joining a list that has Integer values with Python

But solutions like:

', '.join(map(str, myList)) or ', '.join(str(x) for x in list_of_ints)

does not work for me, because special character 'í' makes it fail:

UnicodeEncodeError: 'ascii' codec can't encode character ... in position ...: ordinal not in range(128)

So what is the pythonic way to solve it? I do not want to check types... Thx!

Community
  • 1
  • 1
Alberto Megía
  • 2,225
  • 3
  • 23
  • 33

3 Answers3

2

Checking about encodings I found a solution that works for me:

 u', '.join([unicode(x.decode('utf-8')) if type(x) == type(str()) else unicode(x) for x in a])

The trick is to use decode('utf-8') for getting a valid 8-bit representation of the character.

Hope this will help.

Ander2
  • 5,569
  • 2
  • 23
  • 42
0

If you can specify the strings in your list as unicode, like:

[u"Círculo", 23]
[u"Triángulo", 25, 19, u"dos"]

then this should work:

u', '.join(unicode(x) for x in list_of_ints)

Assuming you are running Python 2.

Blubber
  • 2,214
  • 1
  • 17
  • 26
0
import encodings

a=encodings.utf_16.decode(a[0])
encodings.utf_16.encode(a[0])

Have you tried to do it with the encodings module? Maybe you'll get it to work with this. But doesnt seem to work with the utf_16 encoding...

gspoosi
  • 355
  • 1
  • 9