【问题标题】:How to convert dictionary with integer keys to readable file? [duplicate]如何将带有整数键的字典转换为可读文件? [复制]
【发布时间】:2020-05-31 16:52:59
【问题描述】:

我的代码正在生成一个无法解压不可迭代整数对象的 TypeError

BMI = {
  15: 'underweight',
  23: 'normal weight',
  24: 'normal weight'
  }

with open('output.txt', 'w') as out:
  for k,v in BMI:
    out.write('%s : %s \n' %(k, v))

我想要的文本文件输出如下所示:

15: underweight
23: normal weight
24: normal weight

【问题讨论】:

    标签: python file


    【解决方案1】:

    您需要遍历 dict 项(键和值)。使用for k, v in BMI.items()

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-05-24
      • 1970-01-01
      • 1970-01-01
      • 2014-01-17
      • 2018-08-06
      • 1970-01-01
      • 2013-02-14
      相关资源
      最近更新 更多