python unicode

Python.org


首先感谢python完善的文档,文档从1968年 the American Standard Code (ASCII)开始讲述。

print(type(response))
print(type(response['ERRORCODE']))
print(type(response['RESULT']))

类型分别如下:

<type 'dict'>
<type 'unicode'>
<type 'unicode'>

字典类型(以后把这个栗子再完善点):

dict_demo = {
    "ERRORCODE": unicode(0),
    "RESULT": {
        "accountID": "abc",
        "nickName": "123",
        "tuple": (1,2,3),
        "list": [1,2,3],
        "set":{1,3},
        "dict":{1:1,2:"what?"}
    }
}

最典型的的是根据ERRORCODE的值来判断是否要取“获得“的结果,

origin_data = dict_demo['ERRORCODE']
print("---------------------")
print("before encode utf-8:")
print("value:", origin_data)
print("type", type(origin_data))
utf8_version = origin_data.encode('utf-8')
print("---------------------")
print("after encode utf-8:")
print("value:", utf8_version)
print("type", type(utf8_version))
decode_data = utf8_version.decode('utf-8')
print("---------------------")
print("then decode data back:")
print("value:", decode_data)
print("type", type(decode_data))

结果如下:

---------------------
before encode utf-8:
('value:', u'0')
('type', <type 'unicode'>)
---------------------
after encode utf-8:
('value:', '0')
('type', <type 'str'>)
---------------------
then decode data back:
('value:', u'0')
('type', <type 'unicode'>)

拓展


  1. 更便捷、完善的处理方式?
  2. 中文字符输入、输出
原文地址:https://www.cnblogs.com/dotdog/p/4542782.html