使用自定义分隔符从大文本文件中提取特定分隔符之间的文本部分,并使用Python将其写入另一个文件

时间:2014-01-24 10:15:48

标签: python regex text

我正在开展一个涉及以某种格式创建美国联邦法规数据库的项目。我已经获得了整个代码形式的官方来源,结构不合理。我已经设法使用GITHUB上的一些代码将以下格式的US Code压缩成文本文件。

-CITE-
    13 USC Sec. 1                                               1/15/2013

-EXPCITE-
    TITLE 13 - CENSUS
    CHAPTER 1 - ADMINISTRATION
    SUBCHAPTER I - GENERAL PROVISIONS

-HEAD-
    Sec. 1. Definitions

-STATUTE-
      As used in this title, unless the context requires another
    meaning or unless it is otherwise provided - 
        (1) "Bureau" means the Bureau of the Census;
        (2) "Secretary" means the Secretary of Commerce; and
        (3) "respondent" includes a corporation, company, association,
      firm, partnership, proprietorship, society, joint stock company,
      individual, or other organization or entity which reported
      information, or on behalf of which information was reported, in
      response to a questionnaire, inquiry, or other request of the
      Bureau.

-SOURCE-
    (Aug. 31, 1954, ch. 1158, 68 Stat. 1012; Pub. L. 94-521, Sec. 1,
    Oct. 17, 1976, 90 Stat. 2459.)


-MISC1-
                      <some text>

-End-


-CITE-
    13 USC Sec. 2                                               1/15/2013

-EXPCITE-
    TITLE 13 - CENSUS
    CHAPTER 1 - ADMINISTRATION
    SUBCHAPTER I - GENERAL PROVISIONS

-HEAD-
    Sec. 2. Bureau of the Census

-STATUTE-
      The Bureau is continued as an agency within, and under the
    jurisdiction of, the Department of Commerce.

-SOURCE-
    (Aug. 31, 1954, ch. 1158, 68 Stat. 1012.)


-MISC1-
                      <some text>

-End-

每个文本文件包含数千个这样的块,以-CITE标签开头,以-END-结尾。

除了这些之外,还有某些块代表章节或子章节的开头,而这些块不包含-STATUTE-标签。

E.g。

-CITE-
    13 USC CHAPTER 3 - COLLECTION AND PUBLICATION OF
           STATISTICS                                      1/15/2013

-EXPCITE-
    TITLE 13 - CENSUS
    CHAPTER 3 - COLLECTION AND PUBLICATION OF STATISTICS

-HEAD-
           CHAPTER 3 - COLLECTION AND PUBLICATION OF STATISTICS       


-MISC1-
                           SUBCHAPTER I - COTTON                       
    Sec.                                                     
    41.         Collection and publication.                           
    42.         Contents of reports; number of bales of linter;
                 distribution; publication by Department of
                 Agriculture.                                         
    43.         Records and reports of cotton ginners.                

       SUBCHAPTER II - OILSEEDS, NUTS, AND KERNELS; FATS, OILS, AND
                                  GREASES
    61.         Collection and publication.                           
    62.         Additional statistics.                                
    63.         Duplicate collection of statistics prohibited; access
                 to available statistics.                             

                   SUBCHAPTER III - APPAREL AND TEXTILES               
    81.         Statistics on apparel and textile industries.         

              SUBCHAPTER IV - QUARTERLY FINANCIAL STATISTICS          
    91.         Collection and publication.                           

                       SUBCHAPTER V - MISCELLANEOUS                   
    101.        Defective, dependent, and delinquent classes; crime.  
    102.        Religion.                                             
    103.        Designation of reports.                               

                                AMENDMENTS                            
      <some text>

-End-

我只对那些带有-STATUTE标签的块感兴趣。

有没有办法只提取具有-STATUTE-标记的文本块并将它们写入另一个文本文件?

我是Python的新手,但我被告知这可以在Python中轻松完成。

感谢有人可以指导我。

2 个答案:

答案 0 :(得分:1)

因此,对于每一行,如果它以连字符开头,后跟一些大写文本,后跟另一个连字符,则它是一个标记,表明我们处于某种新的部分。这可以使用正则表达式完成:

current_section_type = None
r= re.compile("^-([A-Z]*)-")
for line in f.readlines():
  m=r.match(line)
  if m:
    current_section_type = m.group(1)
  else:
    if current_section_type == "STATUTE":
      print line.strip()

答案 1 :(得分:0)

我逐行阅读文本并自行解析。这样您就可以将大输入作为流处理。使用多行regexp有更好的解决方案,但这些解决方案总是会因为无法将输入作为流来处理。

#!/usr/bin/env python

import sys, re

# states for our state machine:
OUTSIDE = 0
INSIDE = 1
INSIDE_AFTER_STATUTE = 2

def eachCite(stream):
  state = OUTSIDE
  for lineNumber, line in enumerate(stream):
    if state in (INSIDE, INSIDE_AFTER_STATUTE):
      capture += line
    if re.match('^-CITE-', line):
      if state == OUTSIDE:
        state = INSIDE
        capture = line
      elif state in (INSIDE, INSIDE_AFTER_STATUTE):
        raise Exception("-CITE- in -CITE-??", lineNumber)
      else:
        raise NotImplementedError(state)
    elif re.match('^-End-', line):
      if state == OUTSIDE:
        raise Exception("-End- without -CITE-??", lineNumber)
      elif state == INSIDE:
        yield False, capture
        state = OUTSIDE
      elif state == INSIDE_AFTER_STATUTE:
        yield True, capture
        state = OUTSIDE
      else:
        raise NotImplementedError(state)
    elif re.match('^-STATUTE-', line):
      if state == OUTSIDE:
        raise Exception("-STATUTE- without -CITE-??", lineNumber)
      elif state == INSIDE:
        state = INSIDE_AFTER_STATUTE
      elif state == INSIDE_AFTER_STATUTE:
        raise Exception("-STATUTE- after -STATUTE-??", lineNumber)
      else:
        raise NotImplementedError(state)
  if state != OUTSIDE:
    raise Exception("EOF in -CITE-??")

for withStatute, cite in eachCite(sys.stdin):
  if withStatute:
    print "found cite with statute:"
    print cite

如果您不想处理sys.stdin,可以这样做:

with open('myInputFileName') as myInputFile, \
     open('myOutputFileName', 'w') as myOutputFile:
  for withStatute, cite in eachCite(myInputFile):
    if withStatute:
      myOutputFile.write("found cite with statute:\n")
      myOutputFile.write(cite)