Python -“int”对象不可为“in dict”订阅

vatpfxk5  于 2023-01-01  发布在  Python
关注(0)|答案(2)|浏览(109)

我的代码在一个有几个指令的列表上做了一个for in,代码在第一项上工作,但是当它转到第二项时,错误“'int' object is not subscriptable”发生了因为项“cavalo”不是int,而是一个字符串,我找不到任何能帮助我的东西。

from PyPDF2 import PdfReader, PdfWriter
import re

veiculos = [{"cavalo":"AAA1234","carretas":["CCC1234","DDD1234","EEE1234"]},{"cavalo":"BBB1234","carretas":["FFF1234","GGG1234","HHH1234"]}]

for i in veiculos:
    for conj in i.items():
        
        placa1 = i["cavalo"]
        placa2 = i["carretas"][0]
        placa3 = i["carretas"][1]
        placa4 = i["carretas"][2]

        pdf_file_path = 'IBAMA SP 30-03-2023.pdf'
        file_base_name = pdf_file_path.replace('.pdf', '')
        arquivo = PdfReader(pdf_file_path)
        NumPages = len(arquivo.pages)
        paginas = []
        paginas.append(0)

        for i in range(0, NumPages):
            PageObj = arquivo.pages[i]
            Text = PageObj.extract_text()
            if re.search(placa1,Text):
                paginas.append(i)

        for i in range(0, NumPages):
            PageObj = arquivo.pages[i]
            Text = PageObj.extract_text()
            if re.search(placa2,Text):
                paginas.append(i)

        for i in range(0, NumPages):
            PageObj = arquivo.pages[i]
            Text = PageObj.extract_text()
            if re.search(placa3,Text):
                paginas.append(i)

        for i in range(0, NumPages):
            PageObj = arquivo.pages[i]
            Text = PageObj.extract_text()
            if re.search(placa4,Text):
                paginas.append(i)

        paginas.append(len(arquivo.pages)-1)
        pdfWriter = PdfWriter()

        for page_num in paginas:
            pdfWriter.add_page(arquivo.pages[page_num])

        with open('{0}_subset.pdf'.format(placa1), 'wb') as f:
            pdfWriter.write(f)
            f.close()

我需要“placa1”来接收“AAA1234”,然后是“BBB1234”...

pdsfdshx

pdsfdshx1#

我想知道你会不会运气好

for i in veiculos:
    for conj in i.items():
        # rest of your function

for i,conj in veiculos.items():
    # rest of your function
mklgxw1f

mklgxw1f2#

veiculos = [{"cavalo":"AAA1234","carretas":["CCC1234","DDD1234","EEE1234"]},{"cavalo":"BBB1234","carretas":["FFF1234","GGG1234","HHH1234"]}]
for i in veiculos:
for conj in i.items():
    print(conj)
    #placa1 = i["cavalo"]
    placa1 = conj[1]
    placa2 = i["carretas"][0]
    placa3 = i["carretas"][1]
    placa4 = i["carretas"][2]

相关问题