【问题标题】:Does anyone know how to be able to print this variable?有谁知道如何打印这个变量?
【发布时间】:2022-07-08 01:21:39
【问题描述】:
Code = "00000"
Number = "111-111-1111"
output="{\r\n      \"Code\":\"{0}\",\r\n    \"PhNumber\": \"{1}\"\r\n}".format(Code, Number)

print(output)

不断收到此错误。

Traceback(最近一次调用最后一次): 文件“./prog.py”,第 3 行,在 KeyError: '\r\n "代码"'

【问题讨论】:

    标签: python


    【解决方案1】:

    您需要将整个字符串包裹在一组额外的花括号中,以防止最外面的花括号被解释为格式占位符。

    output = "{{\r\n      \"Code\":\"{0}\",\r\n    \"PhNumber\": \"{1}\"\r\n}}".format(Code, Number)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-11-03
      • 2021-07-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-09-18
      • 2020-10-04
      相关资源
      最近更新 更多