【问题标题】:python : an integer is required (got type str)python:需要一个整数(类型为str)
【发布时间】:2020-07-27 08:00:53
【问题描述】:

我只需要将 int 列表打印为 ASCII。

a=list(str(12345))
for q in a:
    print(chr(q))

需要一个整数(类型为 str)

为什么会出现这个错误?

【问题讨论】:

  • 因为chr 需要一个整数并且你给它一个字符串,我想你可能正在寻找ord(q)
  • chr 需要一个整数,我猜你在找ord
  • a 不是intlist,而是strlist['1', '2', '3', '4', '5'] 特别是。
  • q 是一个字符串,因为您正在遍历字符列表 a
  • 你到底想打印什么?

标签: python


【解决方案1】:

您正在将一个字符串值传递给 chr() 函数。这应该有效:

a=list(str(12345))
for q in a:
    print(chr(int(q)))

#The above code will work but this will print out characters, as 1-5
# in the ASCII table are not visible characters.

a = [65,66,67,68,69]
for q in a:
    print(chr(q))

【讨论】:

  • 那是因为 1、2、3、4 和 5 并不是真正的字符。查看此链接:asciitable.com 如果您想查看字符,请查看上面代码的第二个 sn-p。
猜你喜欢
  • 2017-12-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-04-14
  • 2021-06-14
相关资源
最近更新 更多