如何从列表访问元素的一部分? [英] How to access a part of an element from a list?
问题描述
import cv2
import os
import glob
import pandas as pd
from pylibdmtx import pylibdmtx
import xlsxwriter
# co de for scanning
img_dir = "C:\\images" # Enter Directory of all images
data_path = os.path.join(img_dir,'*g')
files = glob.glob(data_path)
data = []
result=[]
for f1 in files:
img = cv2.imread(f1,cv2.IMREAD_UNCHANGED)
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
ret, thresh = cv2.threshold(gray, 0, 255, cv2.THRESH_BINARY | cv2.THRESH_OTSU)
msg = pylibdmtx.decode(thresh)
print(msg)
data.append(img)
result.append(msg)
print(type(result[0]))
我在列表名称结果中有4个列表的lsit。上面代码的输出是result。该代码旨在读取条形码,但它也提供了我不需要的位置。
I have a lsit of 4 list inside a lists names result .The output of above code is result . The code is intended to read the barcode , but it also provides location which is not required by me .
SO在上述代码之后,我有一个名为result的输出,whch给了我::
SO after the above code , I have a output named result , whch gives me ::
[[Decoded(data=b'AZ:HP7CXNGSUFEPZCO4GS5RQPY6XY', rect=Rect(left=37, top=152, width=94, height=97))], [Decoded(data=b'AZ:RCHKBW5WGZE98J7E9853OW4ZHE', rect=Rect(left=40, top=125, width=91, height=95))], [Decoded(data=b'AZ:5Z7HME1FRNAZFINDPTDAOTB9GQ', rect=Rect(left=27, top=112, width=88, height=88))]
因此,现在我要从所有单个列表中提取或找到az aprt并将其导出到excel。
so NOW i want ot jsut extract or find The az aprt from the all the single lists and export it to excel.
AZ:HP7CXNGSUFEPZCO4GS5RQPY6XY
AZ:RCHKBW5WGZE98J7E9853OW4ZHE
AZ:5Z7HME1FRNAZFINDPTDAOTB9GQ
我只希望上面的输出而忽略所有位置详细信息。
I want only the above output and omit all the location details .
我尝试使用索引编制,但是IT的说法是超出范围。
I have tried with indexing , but IT's saying lists out of range.
请帮助我。
推荐答案
您需要在列表上进行迭代并检索每个列表上的良好属性。
You need to iterate on the list and retrieve the good properties on each.
values = [[Decoded(data=b'AZ:HP7CXNGSUFEPZCO4GS5RQPY6XY', rect=Rect(left=37, top=152, width=94, height=97))],
[Decoded(data=b'AZ:9475EFWZCNARPEJEZEMXDFHIBI', rect=Rect(left=32, top=191, width=90, height=88))],
[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))]]
datas = [value[0].data for value in values] # list of encoded string (b'')
datas = [value[0].data.decode() for value in values] # list of strings
这篇关于如何从列表访问元素的一部分?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!