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

2024-04-24 03:06:13 发布

您现在位置: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-结束。在

除此之外,还有一些块代表一章或一个子章的开头,并且这些块不包含一个-规约-标签。在

例如

^{pr2}$

我只对那些有-法令-标签的区块感兴趣。在

有没有一种方法可以只提取带有-规约-标记的文本块并将它们写入另一个文本文件?在

我是Python新手,但我听说在Python中很容易做到这一点。在

如果有人能指导我,我会很感激。在


Tags: orofthe项目代码title格式sec
2条回答

所以,对于每一行,如果它以一个连字符开头,接着是一些大写文本,然后是另一个连字符,那么它就是一个标记,它指出我们处于某种新的部分中。这可以使用正则表达式来完成:

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

我会逐行阅读文本并自己解析。这样您就可以将大量输入作为流处理。使用多行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,可以这样做:

^{pr2}$

相关问题 更多 >