3

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)'
6
  • 4
    What code are you using to print while iterating? Can we see some more snippets? Commented Feb 19, 2016 at 18:37
  • I got the oposite result. It prints fine when i print one by one and with unicode error when i print the whole list Commented Feb 19, 2016 at 18:45
  • I have the same result as @Pardoido You have to show us Your code to solve this issue. Commented Feb 19, 2016 at 18:47
  • Updated with code examples. Commented Feb 19, 2016 at 19:08
  • Well I'm not really sure what You expect. That's how the strings are stored, when You execute list[0] it shows You the strings with codes for special symbols depending on the string coding, when You run print list[0] it will exchange those codes with exact symbols. Commented Feb 19, 2016 at 19:16

1 Answer 1

7

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:

  1. list[2] instead of list[0]
  2. 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] + Enterlist[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
™
>>> 
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.