<i id='YtGNT'><tr id='YtGNT'><dt id='YtGNT'><q id='YtGNT'><span id='YtGNT'><b id='YtGNT'><form id='YtGNT'><ins id='YtGNT'></ins><ul id='YtGNT'></ul><sub id='YtGNT'></sub></form><legend id='YtGNT'></legend><bdo id='YtGNT'><pre id='YtGNT'><center id='YtGNT'></center></pre></bdo></b><th id='YtGNT'></th></span></q></dt></tr></i><div id='YtGNT'><tfoot id='YtGNT'></tfoot><dl id='YtGNT'><fieldset id='YtGNT'></fieldset></dl></div>
  • <tfoot id='YtGNT'></tfoot>

      • <bdo id='YtGNT'></bdo><ul id='YtGNT'></ul>

        <small id='YtGNT'></small><noframes id='YtGNT'>

        <legend id='YtGNT'><style id='YtGNT'><dir id='YtGNT'><q id='YtGNT'></q></dir></style></legend>

        JSON序列化以元组为键的字典

        JSON serialize a dictionary with tuples as key(JSON序列化以元组为键的字典)

            <tbody id='rFepz'></tbody>
          • <bdo id='rFepz'></bdo><ul id='rFepz'></ul>

            <small id='rFepz'></small><noframes id='rFepz'>

                <i id='rFepz'><tr id='rFepz'><dt id='rFepz'><q id='rFepz'><span id='rFepz'><b id='rFepz'><form id='rFepz'><ins id='rFepz'></ins><ul id='rFepz'></ul><sub id='rFepz'></sub></form><legend id='rFepz'></legend><bdo id='rFepz'><pre id='rFepz'><center id='rFepz'></center></pre></bdo></b><th id='rFepz'></th></span></q></dt></tr></i><div id='rFepz'><tfoot id='rFepz'></tfoot><dl id='rFepz'><fieldset id='rFepz'></fieldset></dl></div>

                <legend id='rFepz'><style id='rFepz'><dir id='rFepz'><q id='rFepz'></q></dir></style></legend>
                  <tfoot id='rFepz'></tfoot>
                  本文介绍了JSON序列化以元组为键的字典的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  在 Python 中有没有办法序列化使用元组作为键的字典?

                  Is there a way in Python to serialize a dictionary that is using a tuple as key?

                  例如

                  a = {(1, 2): 'a'}
                  

                  只需使用 json.dumps(a) 就会引发此错误:

                  simply using json.dumps(a) raises this error:

                  Traceback (most recent call last):
                    File "<stdin>", line 1, in <module>
                    File "/usr/lib/python2.6/json/__init__.py", line 230, in dumps
                      return _default_encoder.encode(obj)
                    File "/usr/lib/python2.6/json/encoder.py", line 367, in encode
                      chunks = list(self.iterencode(o))
                    File "/usr/lib/python2.6/json/encoder.py", line 309, in _iterencode
                      for chunk in self._iterencode_dict(o, markers):
                    File "/usr/lib/python2.6/json/encoder.py", line 268, in _iterencode_dict
                      raise TypeError("key {0!r} is not a string".format(key))
                  TypeError: key (1, 2) is not a string
                  

                  推荐答案

                  你不能将它序列化为 json,json 对于什么是 dict 键的理解远不如 python 灵活.

                  You can't serialize that as json, json has a much less flexible idea about what counts as a dict key than python.

                  您可以将映射转换为一系列键值对,如下所示:

                  You could transform the mapping into a sequence of key, value pairs, something like this:

                  import json
                  def remap_keys(mapping):
                      return [{'key':k, 'value': v} for k, v in mapping.iteritems()]
                  ... 
                  json.dumps(remap_keys({(1, 2): 'foo'}))
                  >>> '[{"value": "foo", "key": [1, 2]}]'
                  

                  这篇关于JSON序列化以元组为键的字典的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  Initialize Multiple Numpy Arrays (Multiple Assignment) - Like MATLAB deal()(初始化多个 Numpy 数组(多重赋值) - 像 MATLAB deal())
                  How to extend Python class init(如何扩展 Python 类初始化)
                  What#39;s the difference between dict() and {}?(dict() 和 {} 有什么区别?)
                  What is a wrapper_descriptor, and why is Foo.__init__() one in this case?(什么是 wrapper_descriptor,为什么 Foo.__init__() 在这种情况下是其中之一?)
                  Initialize list with same bool value(使用相同的布尔值初始化列表)
                  setattr with kwargs, pythonic or not?(setattr 与 kwargs,pythonic 与否?)

                          <tbody id='436RI'></tbody>
                        • <bdo id='436RI'></bdo><ul id='436RI'></ul>
                          <i id='436RI'><tr id='436RI'><dt id='436RI'><q id='436RI'><span id='436RI'><b id='436RI'><form id='436RI'><ins id='436RI'></ins><ul id='436RI'></ul><sub id='436RI'></sub></form><legend id='436RI'></legend><bdo id='436RI'><pre id='436RI'><center id='436RI'></center></pre></bdo></b><th id='436RI'></th></span></q></dt></tr></i><div id='436RI'><tfoot id='436RI'></tfoot><dl id='436RI'><fieldset id='436RI'></fieldset></dl></div>
                            <legend id='436RI'><style id='436RI'><dir id='436RI'><q id='436RI'></q></dir></style></legend>

                            <small id='436RI'></small><noframes id='436RI'>

                          • <tfoot id='436RI'></tfoot>