我的问题字符串
# -*- 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
这个很好 运作良好,我只是用键盘手动输入字符串“CT142H.0000”(这是相同的代码)。
print ("################################")
foo = "CT142H.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: CT142H.0000
List:
['CT142H.0000']
List decode:
[u'CT142H.0000']
Pop: CT142H.0000
为什么当我把第一个字符串添加到列表中时,Python会对其进行编码?
-----------------------------------------------
目前已经解决了,我担心那些字符,所以我将“结果”放在 JSON 中,然后在网站上使用,最终在网站上它可以正常工作!
-----------------------------------------------
我找到了另一种解决方案,但这不是正确的解决方案,因为在某些情况下会有问题。
json.dumps(list, ensure_ascii=False)
感谢您的支持!