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

2

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

2

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật