Map object has no len() in Python 3

前端 未结 1 1080
-上瘾入骨i
-上瘾入骨i 2020-12-06 08:51

I have this Python tool written by someone else to flash a certain microcontroller, but he has written this tool for Python 2.6 and I am using Python 3.3.

So, most o

相关标签:
1条回答
  • 2020-12-06 09:53

    In Python 3, map returns an iterator. If your function expects a list, the iterator has to be explicitly converted, like this:

    data = list(map(...))
    

    And we can do it simply, like this

    with open(args[0], "rb") as input_file:
        data = list(input_file.read())
    

    rb refers to read in binary mode. So, it actually returns the bytes. So, we just have to convert them to a list.

    Quoting from the open's docs,

    Python distinguishes between binary and text I/O. Files opened in binary mode (including 'b' in the mode argument) return contents as bytes objects without any decoding.

    0 讨论(0)
提交回复
热议问题