Search code examples
pythonregexregex-group

error when executing regular expression with python


the file "catalogo_no_linebreak.txt" contains a list of products, grouped in one line, i'am using the regular expression (re.split()) to try Retrieve the record of each product and logo after saving to the "saida.txt" file.

The txt_format() function is where I process and use the regular expression "result = re.split(r',00', arq.readline())".

The way I'm using the regular expression with product records that are returning incomplete, we can see it highlighted in red.

How can I retrieve the content of each product that is in the pattern "0453. [content] ,00"?

saida.txt

0453.000045-00453.213.00022733-0UMA ALIANÇA, DE: OURO,  OURO BRANCO; CONSTAM: amolgada(s), PESOLOTE: 4,40G (QUATRO GRAMAS E QUARENTA CENTIGRAMAS)R$ 652,00
0453.000047-70453.213.00022959-6DUAS ALIANÇASCOM 3 ELOS CADA, DE: OURO; CONSTAM: amolgada(s),inscrições, PESO LOTE: 4
G (QUATRO GRAMAS)R$ 529,00
0453.000053-10453.213.00023492-1QUATRO ANÉIS, DOIS BRINCOS, UMA PULSEIRA, DE: OURO,  OURO BAIXO,2,93G; CONTÉM: diamantes,  massa,  pedra branca,  pedras; CONSTAM:amolgada(s), PESO LOTE: 31,98G (TRINTA E UM GRAMAS E NOVENTA E OITOCENTIGRAMAS)R$ 2.196,00

output saida.txt product records return "broken"

catalogo_no_linebreak.txt

0453.000001-90453.213.00000175-7DUAS ALIANÇAS, SEIS ANÉIS, DUAS PULSEIRAS, DE: OURO BRANCO,  OURO;CONTÉM: diamantes,  pedras,  pérola cultivada; CONSTAM: amolgada(s),inscrições, PESO LOTE: 17,99G (DEZESSETE GRAMAS E NOVENTA E NOVECENTIGRAMAS)0453.000002-70453.213.00000571-0UM ANEL, DOIS BRINCOS, UM COLAR, UM PENDENTE, DE: OURO; CONTÉM:pérola cultivada, PESO LOTE: 4,82G (QUATRO GRAMAS E OITENTA E DOISCENTIGRAMAS)R$ 623,000453.000005-10453.213.00001496-4UM ALFINETE, TRES ANÉIS, DOIS BRINCOS, QUATRO COLARES, UMPENDENTE, DUAS PULSEIRAS, DE: OURO BRANCO,  OURO; CONTÉM: coral,pérola cultivada,  diamantes, 1 D BRI KL VS APROX 0,25CT E 1 D LAP BRASIL KLVS APROX 0,40CT CC, PESO LOTE: 81,70G (OITENTA E UM GRAMAS ESETENTA CENTIGRAMAS)R$ 10.587,00

convertPdfToTxt.py

from PyPDF2 import PdfReader
import re
import os.path

pdf_reader = PdfReader("catalogo.pdf")
parts = []

def set_number_of_pages():
    total_pages = len(pdf_reader.pages)
    valid_pages = total_pages - 2
    return valid_pages

def get_number_of_pages():
   return set_number_of_pages()

def visitor_body(text, cm, tm, fontDict, fontSize):
    y = tm[5]
    if y > 0 and y < 750:
        parts.append(text)

def txt_save():
    numberOfpages = get_number_of_pages()

    for i in range(1, numberOfpages):
        page = pdf_reader.pages[i]
        page.extract_text(visitor_text=visitor_body)
        text_body = "".join(parts)

    with open("catalogo.txt", mode='a+', encoding='utf-8') as file:
        file.write(text_body + "\n")

def remove_line_break():
    file = open("catalogo.txt", mode="r", encoding="utf-8")

    for line in file.readlines():
        a = line.rstrip('\n')
        with open("catalogo_no_linebreak.txt", mode='a+', encoding='utf-8') as arq:
            arq.write('{}'.format(a))
    file.close()

def txt_format():
    with open('catalogo_no_linebreak.txt', mode='r', encoding='utf-8') as arq:
        result = re.split(r',00', arq.readline())

        for item in result:
            with open('saida.txt', mode='a+', encoding='utf-8') as arq:
                arq.write(item + '\n')

def delete_files():
    file_catalog = os.path.isfile('catalogo.txt')
    file_catalog_no_linebreak = os.path.isfile('catalogo_no_linebreak.txt')

    if file_catalog:
        os.remove('catalogo.txt')

    if file_catalog_no_linebreak:
        os.remove('catalogo_no_linebreak.txt')

def convert_to_txt():
    txt_save()
    remove_line_break()
    txt_format()

def start():
    if pdf_reader:
        print('Encontrou o catalogo!')
        convert_to_txt()
        delete_files()

start()

Solution

  • This code assumes that each product line starts with an ID of the same format. then separate the text using the ID and print the ID and Content without line breaks

    Solution

    import re
    
    content = """
    0453.000045-00453.213.00022733-0UMA ALIANÇA, DE: OURO,  OURO BRANCO; CONSTAM: amolgada(s), PESOLOTE: 4,40G (QUATRO GRAMAS E QUARENTA CENTIGRAMAS)R$ 652,00
    0453.000047-70453.213.00022959-6DUAS ALIANÇASCOM 3 ELOS CADA, DE: OURO; CONSTAM: amolgada(s),inscrições, PESO LOTE: 4
    G (QUATRO GRAMAS)R$ 529,00
    0453.000053-10453.213.00023492-1QUATRO ANÉIS, DOIS BRINCOS, UMA PULSEIRA, DE: OURO,  OURO BAIXO,2,93G; CONTÉM: diamantes,  massa,  pedra branca,  pedras; CONSTAM:amolgada(s), PESO LOTE: 31,98G (TRINTA E UM GRAMAS E NOVENTA E OITOCENTIGRAMAS)R$ 2.196,00
    """
    
    pattern_base=r'(\d{4}\.\d{6}-\d{5}\.\d{3}\.\d{8}-\w+)'
    
    result = re.split(pattern_base,content)
    
    for section in result:
        if section != "\n":
            if re.match(pattern_base, section):
                print('head: '+ section)
            else :
                section = section.replace("\n", " ")
                section = section.replace("  ", " ")
                section = section.strip()
                print('content: '+section)
    
    

    Output

    head: 0453.000045-00453.213.00022733-0UMA
    content: ALIANÇA, DE: OURO, OURO BRANCO; CONSTAM: amolgada(s), PESOLOTE: 4,40G (QUATRO GRAMAS E QUARENTA CENTIGRAMAS)R$ 652,00
    head: 0453.000047-70453.213.00022959-6DUAS
    content: ALIANÇASCOM 3 ELOS CADA, DE: OURO; CONSTAM: amolgada(s),inscrições, PESO LOTE: 4 G (QUATRO GRAMAS)R$ 529,00
    head: 0453.000053-10453.213.00023492-1QUATRO
    content: ANÉIS, DOIS BRINCOS, UMA PULSEIRA, DE: OURO, OURO BAIXO,2,93G; CONTÉM: diamantes, massa, pedra branca, pedras; CONSTAM:amolgada(s), PESO LOTE: 31,98G (TRINTA E UM GRAMAS E NOVENTA E OITOCENTIGRAMAS)R$ 2.196,00