【问题标题】:How to extract Decoded data from a list?如何从列表中提取解码数据?
【发布时间】:2020-09-12 17:38:25
【问题描述】:
[[Decoded(data=b'AZ:HP7CXNGSUFEPZCO4GS5RQPY6XY', rect=Rect(left=37, top=152, width=94, height=97)),Decoded(data=b'AZ:1EBWZS6F15BDFNK7SK5Y5N3X5U', rect=Rect(left=2616, top=1414, width=108, height=122))], [Decoded(data=b'AZ:9475EFWZCNARPEJEZEMXDFHIBI', rect=Rect(left=32, top=191, width=90, height=88)),Decoded(data=b'AZ:1EBWZS6F15BDFNK7SK5Y5N3X5U', rect=Rect(left=2616, top=1414, width=108, height=122))], [Decoded(data=b'AZ:6ECWZUQGEJCR5EZXDH9URCN53M', rect=Rect(left=48, top=183, width=88, height=89))], [Decoded(data=b'AZ:XZ9P6KTDGREM5KIXUO9IHCTKAQ', rect=Rect(left=73, top=121, width=91, height=94))]]

我有一个类似上面的列表,上面的列表名为result。

现在我只想从列表中的所有列表中提取 AZ:HP7CXNGSUFEPZCO4GS5RQPY6XY 。

我试过了

循环检测到的条形码

for barcode in result:

    # the barcode data is a bytes object so if we want to draw it on
    # our output image we need to convert it to a string first
    barcodeData = barcode.data.decode("utf-8")

但是我收到了list object has no attribute "data ",所以我很困惑如何只提取重要信息。

【问题讨论】:

    标签: python list image barcode-scanner nested-lists


    【解决方案1】:

    在您的示例中,条形码数据嵌套在单个元素列表中,因此您必须在创建列表期间注意这一点,或者使用 [0] 正确寻址嵌套列表的索引 0:

    data_list = list()
    for entry in result:
        for barcode in entry:
            data_list.append(barcode.data.decode("utf-8"))
    

    【讨论】:

    • 效果很好,我错过了 data_list 部分。感谢您的帮助
    • 当在结果 lsit 中,他们是一个空行,我得到 Traceback(最近一次调用最后):文件“”,第 57 行,在 IndexError: list index out范围误差
    • 在单行中,它们是AZ代码的2个或多个案例我该怎么办??当列表中每行只有一个 AZ 代码时,上面的代码运行良好
    • TypeError: 'int' object is not iterable 我在使用代码时遇到错误
    • 听起来您的结果列表中包含混合类型。您可能需要查看生成它的代码以确保您有一致的输入
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-10-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多