从大型文本文件中提取文本的部分,其中包括自定义定系数,然后使用Python将其写入另一个文件



我正在研究一个项目,该项目涉及以某种格式创建美国联邦代码数据库。我获得了整个代码形式的官方资源,该来源的结构不佳。我设法使用github上的某些代码将以下格式刮擦到文本文件中。

-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 -tag开头的数千个这样的块,并以-End-结尾。

除此之外,还有某些代表章节或子章的开始,而这些块不包含-statute- tag。

例如。

-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 -tag的块感兴趣。

是否有一种方法仅提取具有-statute-标签的文本块并将其写入另一个文本文件?

我是Python的新手

感谢有人可以指导我。

因此,对于每一行,如果它以连字符开头,然后是一些上案例文本,然后是另一个连字符,然后是一个标记,指出我们正在新的某种部分。这可以使用正则表达式完成:

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

我会逐行阅读文本行,然后自己解析。这样,您可以处理大型输入作为流。使用多行左右有更好的解决方案,但这些解决方案总是无法将输入作为流进行处理。

#!/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)

最新更新