I have a list with names. Some of them consists of strange chars like ★ or ™. When I am iterating threw list, it prints just fine:
★ StatTrak™ Huntsman Knife | Safari Mesh (Battle-Scarred)
Souvenir USP-S | Night Ops (Well-Worn)
StatTrak™ G3SG1 | The Executioner (Minimal Wear)
However, when I try to print it one by one:
print a[0]
'\xe2\x98\x85 StatTrak\xe2\x84\xa2 Huntsman Knife | Safari Mesh (Battle-Scarred)'
How to solve this problem?
UPDATE:
Iterating:
list = ['★ StatTrak™ Huntsman Knife | Safari Mesh (Battle-Scarred)',
'Souvenir USP-S | Night Ops (Well-Worn)',
'StatTrak™ G3SG1 | The Executioner (Minimal Wear)']
for name in list:
print name
>>>
★ StatTrak™ Huntsman Knife | Safari Mesh (Battle-Scarred)
Souvenir USP-S | Night Ops (Well-Worn)
StatTrak™ G3SG1 | The Executioner (Minimal Wear)
However:
list[0]
>>>
'StatTrak\xe2\x84\xa2 G3SG1 | The Executioner (Minimal Wear)'
There is no problem with your situation, I think you just need a little clarification. But first, you have 2 typos I would love you to correct:
list[2] instead of list[0]a[0] instead of print a[0]When you type list[2] directly, you get this output:
'StatTrak\xe2\x84\xa2 G3SG1 | The Executioner (Minimal Wear)'
That is because:
list[2] + Enter ↔ list[2].__repr__() + Enter
I mean you get the UTF-8 representation of list[2]. Note that Python picks up this UTF-8 representation from the environment it's been initiated from and which you can check by typing:
>>> import sys
>>> print sys.stdout.encoding
UTF-8
But if you type print list[2] you get:
>>> print list[2]
StatTrak™ G3SG1 | The Executioner (Minimal Wear)
>>>
That is because when you call print with a bystring Python convert to to unicode first. I mean:
print list[2]
↕ (is equivalent to)
print b"'StatTrak\xe2\x84\xa2 G3SG1 | The Executioner (Minimal Wear)'".decode('utf-8')
Demo:
>>> print b"'StatTrak\xe2\x84\xa2 G3SG1 | The Executioner (Minimal Wear)'".decode('utf-8')
'StatTrak™ G3SG1 | The Executioner (Minimal Wear)'
>>>
Simply:
>>> a='★ '
>>> b='™'
>>> a
'\xe2\x98\x85 '
>>> b
'\xe2\x84\xa2'
>>> print b"'\xe2\x98\x85 '".decode('utf-8')
'★ '
>>> print b"'\xe2\x84\xa2'".decode('utf-8')
'™'
>>> print a
★
>>> print b
™
>>>
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