如何打印奇怪的字符串字符?

Irm*_*nis 3 python

我有一个名字的清单.其中一些由像★或™这样的奇怪字符组成.当我迭代扔列表时,打印就好了:

? StatTrak™ Huntsman Knife | Safari Mesh (Battle-Scarred)
Souvenir USP-S | Night Ops (Well-Worn)
StatTrak™ G3SG1 | The Executioner (Minimal Wear)
Run Code Online (Sandbox Code Playgroud)

但是,当我尝试逐个打印时:

print a[0]
'\xe2\x98\x85 StatTrak\xe2\x84\xa2 Huntsman Knife | Safari Mesh (Battle-Scarred)'
Run Code Online (Sandbox Code Playgroud)

如何解决这个问题呢?

更新:

迭代:

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)
Run Code Online (Sandbox Code Playgroud)

然而:

list[0]
>>> 
'StatTrak\xe2\x84\xa2 G3SG1 | The Executioner (Minimal Wear)'
Run Code Online (Sandbox Code Playgroud)

Bil*_*adj 7

你的情况没有问题,我想你只需要一点澄清.但首先,你有2个错别字我希望你纠正:

  1. list[2] 代替 list[0]
  2. a[0] 代替 print a[0]

list[2]直接键入时,您将获得此输出:

'StatTrak\xe2\x84\xa2 G3SG1 | The Executioner (Minimal Wear)'

那是因为:

list[2]+ Enterlist[2].__repr__()+Enter

我的意思是你得到UTF-8表示list[2].请注意,Python从它所启动的环境中获取此UTF-8表示,您可以通过键入以下内容来检查:

>>> import sys
>>> print sys.stdout.encoding
UTF-8
Run Code Online (Sandbox Code Playgroud)

但如果你打字,print list[2]你得到:

>>> print list[2]
StatTrak™ G3SG1 | The Executioner (Minimal Wear)
>>> 
Run Code Online (Sandbox Code Playgroud)

那是因为当你使用bystring调用print时,首先转换为unicode.我的意思是:

print list[2]
Run Code Online (Sandbox Code Playgroud)

(相当于)

print b"'StatTrak\xe2\x84\xa2 G3SG1 | The Executioner (Minimal Wear)'".decode('utf-8')
Run Code Online (Sandbox Code Playgroud)

演示:

>>> print b"'StatTrak\xe2\x84\xa2 G3SG1 | The Executioner (Minimal Wear)'".decode('utf-8')
'StatTrak™ G3SG1 | The Executioner (Minimal Wear)'
>>> 
Run Code Online (Sandbox Code Playgroud)

只是:

>>> 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
™
>>> 
Run Code Online (Sandbox Code Playgroud)