我从一个要调试的项目中获得了Python脚本,但是无法解决一个错误。根据作者对项目的描述,一切正常。
该脚本采用名为“ ascii”的参数,其类型为str,如下所示:
parser.add_argument('--ascii', type=str,
help='ASCII Data type: ASCII characters')
据我所知,在下面的代码中,它一次处理一个字符的输入字符串,并将每个字符发送到一个函数iter_bin(),该函数将采用字符的ASCII值并将其转换为二进制,并追加输出到列表。
ASCIIDATA = args.ascii
dataArray = []
for line in ASCIIDATA:
for entry in line:
# Make sure everything is a number, convert if not
dataArray.append(''.join(s for s in iter_bin(entry)))
def iter_bin(s):
sb = s.encode('ascii')
return (format(b, '07b') for b in sb)
运行此代码时,出现以下错误:
Traceback (most recent call last):
File "check.py", line 107, in <module>
main()
File "check.py", line 70, in main
dataArray.append(''.join(s for s in iter_bin(entry)))
File "check.py", line 70, in <genexpr>
dataArray.append(''.join(s for s in iter_bin(entry)))
File "check.py", line 82, in <genexpr>
return (format(b, '07b') for b in sb)
ValueError: Unknown format code 'b' for object of type 'str'
如何解决此错误?
谢谢。