python3-TypeError:';地图';对象不可下标

python3-TypeError:';地图';对象不可下标,python,Python,我目前正在将此作为i/o流代码的一部分运行-我得到以下错误TypeError:“map”对象不可订阅打印(bytest[:10])。在Python3中运行它的正确方法是什么 with open("/bin/ls", "rb") as fin: #rb as text file and location buf = fin.read() bytes = map(ord, buf) print (bytes[:10]) saveFile = open('exampleFile.txt'

我目前正在将此作为i/o流代码的一部分运行-我得到以下错误TypeError:“map”对象不可订阅打印(bytest[:10])。在Python3中运行它的正确方法是什么

with open("/bin/ls", "rb") as fin: #rb as text file and location 
  buf = fin.read()
bytes = map(ord, buf)    
print (bytes[:10])
saveFile = open('exampleFile.txt', 'w')
saveFile.write(bytes)
saveFile.close()
在Python 3中,返回一个生成器。首先尝试从它创建一个新的

with open("/bin/ls", "rb") as fin: #rb as text file and location 
  buf = fin.read()
bytes = list(map(ord, buf))
print (bytes[:10])
saveFile = open('exampleFile.txt', 'w')
saveFile.write(bytes)
saveFile.close()
如果您认为这很难看,可以将其替换为列表生成器:

bytes = [ord(b) for f in buf]

你可以使用@PeterWood Yes,如果你只需要阅读一次特定的片段,那可能会更好。