Search code examples
pythonlistimagebarcode-scannernested-lists

How to extract Decoded data from a list?


[[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))]]

I have a lists of lists like above, the above list is named result.

Now I want to extract only AZ:HP7CXNGSUFEPZCO4GS5RQPY6XY , from all lists from the list .

I have tried

loop over the detected barcodes

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")

But I'm getting list object has no attribute "data ", so I'm confused to how only extract important info.


Solution

  • From your example, the barcode data is nested inside a single element list, so you would have to take care of that during list creation, or correctly address index 0 of the nested list, with [0]:

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