首页 > 解决方案 > python get() 函数为 str 类和类型 str/class int 和 int 类型提供不同的值

问题描述

我正在尝试将 python 2 代码转换为 python 3,并且刚刚知道两个版本上 type() 函数的区别。我基本上是使用熊猫从 json 文件中提取数据。我有一个变量word = 0x00000010.,我想从 json 文件中获取 word 的值。

所以我的代码在下面

json_data = [Basically this variable have the pulled data from json file]

json_data.get(int(str(word),16), 'none')给出 python 2 中存在的值,但在 python 3 中它返回nan. 我假设它与为 python 3 提供类的 type(str(word)) 有关

我已经看到了许多与这种差异相关的问题,但实际上并没有解决我的问题。

json文件格式:

{
  "color" : "red",
  "sets" :
      {
        "0x00000010":"yellow",
        "0x00000020":"red
      }
}

并且 json_data 包含“集合”中的字典

标签: pythonpython-3.x

解决方案


推荐阅读