Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 110221
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T02:11:34+00:00 2026-05-11T02:11:34+00:00

I need to read some large files (from 50k to 100k lines), structured in

  • 0

I need to read some large files (from 50k to 100k lines), structured in groups separated by empty lines. Each group start at the same pattern ‘No.999999999 dd/mm/yyyy ZZZ’. Here´s some sample data.

No.813829461 16/09/1987 270
Tit.SUZANO PAPEL E CELULOSE S.A. (BR/BA)
C.N.P.J./C.I.C./N INPI : 16404287000155
Procurador: MARCELLO DO NASCIMENTO

No.815326777 28/12/1989 351
Tit.SIGLA SISTEMA GLOBO DE GRAVACOES AUDIO VISUAIS LTDA (BR/RJ)
C.N.P.J./C.I.C./NºINPI : 34162651000108
Apres.: Nominativa ; Nat.: De Produto
Marca: TRIO TROPICAL
Clas.Prod/Serv: 09.40
*DEFERIDO CONFORME RESOLUÇÃO 123 DE 06/01/2006, PUBLICADA NA RPI 1829, DE 24/01/2006.
Procurador: WALDEMAR RODRIGUES PEDRA

No.900148764 11/01/2007 LD3
Tit.TIARA BOLSAS E CALÇADOS LTDA
Procurador: Marcia Ferreira Gomes
*Escritório: Marcas Marcantes e Patentes Ltda
*Exigência Formal não respondida Satisfatoriamente, Pedido de Registro de Marca considerado inexistente, de acordo com Art. 157 da LPI
*Protocolo da Petição de cumprimento de Exigência Formal: 810080140197

I wrote some code that´s parsing it accordingly. There´s anything that I can improve, to improve readability or performance? Here´s what I come so far:

import re, pprint  class Despacho(object):     '''     Class to parse each line, applying the regexp and storing the results     for future use     '''     regexp = {         re.compile(r'No.([\d]{9})  ([\d]{2}/[\d]{2}/[\d]{4})  (.*)'): lambda self: self._processo,         re.compile(r'Tit.(.*)'): lambda self: self._titular,         re.compile(r'Procurador: (.*)'): lambda self: self._procurador,         re.compile(r'C.N.P.J./C.I.C./N INPI :(.*)'): lambda self: self._documento,         re.compile(r'Apres.: (.*) ; Nat.: (.*)'): lambda self: self._apresentacao,         re.compile(r'Marca: (.*)'): lambda self: self._marca,         re.compile(r'Clas.Prod/Serv: (.*)'): lambda self: self._classe,         re.compile(r'\*(.*)'): lambda self: self._complemento,     }      def __init__(self):         '''         'complemento' is the only field that can be multiple in a single registry         '''         self.complemento = []      def _processo(self, matches):         self.processo, self.data, self.despacho = matches.groups()      def _titular(self, matches):         self.titular = matches.group(1)      def _procurador(self, matches):         self.procurador = matches.group(1)      def _documento(self, matches):         self.documento = matches.group(1)      def _apresentacao(self, matches):         self.apresentacao, self.natureza = matches.groups()      def _marca(self, matches):         self.marca = matches.group(1)      def _classe(self, matches):         self.classe = matches.group(1)      def _complemento(self, matches):         self.complemento.append(matches.group(1))      def read(self, line):         for pattern in Despacho.regexp:             m = pattern.match(line)             if m:                 Despacho.regexp[pattern](self)(m)   def process(rpi):     '''     read data and process each group     '''     rpi = (line for line in rpi)     group = False      for line in rpi:         if line.startswith('No.'):             group = True             d = Despacho()                  if not line.strip() and group: # empty line - end of block             yield d             group = False          d.read(line)   arquivo = open('rm1972.txt') # file to process for desp in process(arquivo):     pprint.pprint(desp.__dict__)     print('--------------') 
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. 2026-05-11T02:11:35+00:00Added an answer on May 11, 2026 at 2:11 am

    That is pretty good. Below some suggestions, let me know if you like’em:

    import re import pprint import sys  class Despacho(object):     '''     Class to parse each line, applying the regexp and storing the results     for future use     '''     #used a dict with the keys instead of functions.     regexp = {         ('processo',           'data',           'despacho'): re.compile(r'No.([\d]{9})  ([\d]{2}/[\d]{2}/[\d]{4})  (.*)'),         ('titular',): re.compile(r'Tit.(.*)'),         ('procurador',): re.compile(r'Procurador: (.*)'),         ('documento',): re.compile(r'C.N.P.J./C.I.C./N INPI :(.*)'),         ('apresentacao',          'natureza'): re.compile(r'Apres.: (.*) ; Nat.: (.*)'),         ('marca',): re.compile(r'Marca: (.*)'),         ('classe',): re.compile(r'Clas.Prod/Serv: (.*)'),         ('complemento',): re.compile(r'\*(.*)'),     }      def __init__(self):         '''         'complemento' is the only field that can be multiple in a single registry         '''         self.complemento = []       def read(self, line):         for attrs, pattern in Despacho.regexp.iteritems():             m = pattern.match(line)             if m:                 for groupn, attr in enumerate(attrs):                     # special case complemento:                     if attr == 'complemento':                         self.complemento.append(m.group(groupn + 1))                     else:                         # set the attribute on the object                         setattr(self, attr, m.group(groupn + 1))      def __repr__(self):         # defines object printed representation         d = {}         for attrs in self.regexp:             for attr in attrs:                 d[attr] = getattr(self, attr, None)         return pprint.pformat(d)  def process(rpi):     '''     read data and process each group     '''     #Useless line, since you're doing a for anyway     #rpi = (line for line in rpi)     group = False      for line in rpi:         if line.startswith('No.'):             group = True             d = Despacho()                  if not line.strip() and group: # empty line - end of block             yield d             group = False          d.read(line)  def main():     arquivo = open('rm1972.txt') # file to process     for desp in process(arquivo):         print desp # can print directly here.         print('-' * 20)     return 0  if __name__ == '__main__':     main() 
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to read from a variety of different text files (I've some delimited
I need to read potentially large (~300mb) XML files, and edit some of the
I have some large csv files (1.5gb each) where I need to replace specific
I need to read in two large files (over 125 MB). Each file contains
I need to read selected files, matching on the file name, from a remote
I need to serve up large files (> 2gb) from an Apache web server.
I have some large files (images and video) which I need to store in
I need to read some filenames from an xml config file using xmlstarlet with
I need some help ... I'm a bit (read total) n00b when it comes
I read the SDK document, but can not understand some details, need some help.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.