为什么json.loads返回一个unicode对象而不是字符串(Why json.loads is returning a unicode object instead of string)

编程入门 行业动态 更新时间:2024-10-26 06:35:20
为什么json.loads返回一个unicode对象而不是字符串(Why json.loads is returning a unicode object instead of string)

我无法理解为什么以下类型从str更改为unicode。

情况1

Python 2.7 (r27:82500, Nov 19 2014, 18:07:42) [GCC 4.5.1 20100924 (Red Hat 4.5.1-4)] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>> import json >>> x = {'resources': {}, 'tags': ['a', 'b']} >>> ret = json.dumps( x ) >>> ret '{"resources": {}, "tags": ["a", "b"]}' >>> >>> type( ret ) <type 'str'> >>> ret2 = json.loads( ret ) >>> ret2 {'resources': {}, 'tags': ['a', 'b']}

CASE2

Python 2.7.5 (default, Apr 22 2015, 21:27:15) [GCC 4.9.2 20141101 (Red Hat 4.9.2-1)] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>> import json >>> x = {'resources': {}, 'tags': ['a', 'b']} >>> ret = json.dumps( x ) >>> ret '{"resources": {}, "tags": ["a", "b"]}' >>> type( x ) <type 'dict'> >>> type( ret ) <type 'str'> >>> ret2 = json.loads( ret ) >>> ret2 {u'resources': {}, u'tags': [u'a', u'b']} >>>

因此,在案例2中我们看到unicode对象,就像在案例1中一样,我们看到了字符串。 我没有看到在两个版本的python中发生了任何代码更改,这可能导致这种情况。 可能是我错过了什么。 任何线索将不胜感激。 谢谢

I cannot understand why the following type changes to unicode from str.

CASE1

Python 2.7 (r27:82500, Nov 19 2014, 18:07:42) [GCC 4.5.1 20100924 (Red Hat 4.5.1-4)] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>> import json >>> x = {'resources': {}, 'tags': ['a', 'b']} >>> ret = json.dumps( x ) >>> ret '{"resources": {}, "tags": ["a", "b"]}' >>> >>> type( ret ) <type 'str'> >>> ret2 = json.loads( ret ) >>> ret2 {'resources': {}, 'tags': ['a', 'b']}

CASE2

Python 2.7.5 (default, Apr 22 2015, 21:27:15) [GCC 4.9.2 20141101 (Red Hat 4.9.2-1)] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>> import json >>> x = {'resources': {}, 'tags': ['a', 'b']} >>> ret = json.dumps( x ) >>> ret '{"resources": {}, "tags": ["a", "b"]}' >>> type( x ) <type 'dict'> >>> type( ret ) <type 'str'> >>> ret2 = json.loads( ret ) >>> ret2 {u'resources': {}, u'tags': [u'a', u'b']} >>>

So, in the case 2 we see unicode objects where as in case 1, we see string instead. I don't see any code change has happened in the two version of python that can lead to this. May be I missed something. Any leads would be appreciated. Thanks

最满意答案

Python的2.7版本有一个错误导致第一个例子中的行为。 这在2.7.5中得到修复。 见问题10038 。 请注意,版本2.6.6的行为类似于2.7.5,表明2.7行为是对先前建立的行为的更改。

我不认为任何代码更改都发生在两个版本的python中,可能导致这种情况发生。

当你可以检查并确定时,无需“思考”没有任何改变! Python的每个版本都附带了大量的注释,指出了确切的变化。 在Python 2.7.5 更改日志中 ,术语“json”出现了28次。 当然,也可以在Python 2.7.1,2.7.2,2.7.3和2.7.4中对JSON进行更改。

Version 2.7 of Python had a bug that caused the behavior in your first example. This was fixed in 2.7.5. See issue 10038. Note that version 2.6.6 behaves like 2.7.5, indicating that the 2.7 behavior was a change from the previously-established behavior.

I don't think that any code change has happened in the two version of python that can lead to this.

No need to "think" nothing changed when you can check and be sure! Every release of Python comes with extensive notes indicating exactly what changed. The term "json" appears twenty-eight times in the Python 2.7.5 change log. Changes could also have been made to JSON in Python 2.7.1, 2.7.2, 2.7.3, and 2.7.4, of course.

更多推荐

本文发布于:2023-07-27 17:54:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1293617.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:字符串   而不是   对象   unicode   loads

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!