Python 2.7-为什么Python在列表中添加.append()时对字符串进行编码?
我的问题字符串Python 2.7-为什么Python在列表中添加.append()时对字符串进行编码?,python,string,list,append,Python,String,List,Append,我的问题字符串 # -*- coding: utf-8 -*- print ("################################") foo = "СТ142Н.0000" print (type(foo)) print("foo: "+foo) foo_l = [] foo_l.append(foo) print ("List: " ) print (foo_l) print ("List
# -*- coding: utf-8 -*-
print ("################################")
foo = "СТ142Н.0000"
print (type(foo))
print("foo: "+foo)
foo_l = []
foo_l.append(foo)
print ("List: " )
print (foo_l)
print ("List decode: ")
print([x.decode("UTF-8") for x in foo_l])
print("Pop: "+foo_l.pop())
打印结果:
################################
<type 'str'>
foo: СТ142Н.0000
List:
['\xd0\xa1\xd0\xa2142\xd0\x9d.0000']
List decode:
[u'\u0421\u0422142\u041d.0000']
Pop: СТ142Н.0000
################################
<type 'str'>
foo: CT142H.0000
List:
['CT142H.0000']
List decode:
[u'CT142H.0000']
Pop: CT142H.0000
打印结果:
################################
<type 'str'>
foo: СТ142Н.0000
List:
['\xd0\xa1\xd0\xa2142\xd0\x9d.0000']
List decode:
[u'\u0421\u0422142\u041d.0000']
Pop: СТ142Н.0000
################################
<type 'str'>
foo: CT142H.0000
List:
['CT142H.0000']
List decode:
[u'CT142H.0000']
Pop: CT142H.0000
谢谢大家 因为尽管它们看起来像普通的
C
/T
/H
字符,但实际上它们不是这些字符
他们是西里尔文字
С-⑦-
Б-
你需要检查这些角色是从哪里来的,以及它们为什么会这样 关于它们为什么使用
\x..
表示法打印,是因为当您打印列表时,会调用列表上的\uuuuu str\uuuu()
方法,但列表本身会调用其元素上的\uuuuu repr\uuuuuu()
,因此您可以得到字符串的内部表示法。如果你这样做,你会得到类似的结果-
print(repr(foo))
对于第一种情况。我从一个网站上获取字符串,正在进行web报废。我会看看为什么我会得到这个。谢谢你的回答,