我正在做一个项目,这个项目包括以某种格式创建一个美国联邦代码的数据库。我已经从官方来源获得了完整的代码,它的结构不好。我已经设法在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中很容易做到这一点。在
如果有人能指导我,我会很感激。在
所以,对于每一行,如果它以一个连字符开头,接着是一些大写文本,然后是另一个连字符,那么它就是一个标记,它指出我们处于某种新的部分中。这可以使用正则表达式来完成:
我会逐行阅读文本并自己解析。这样您就可以将大量输入作为流处理。使用多行regexp有更好的解决方案,但是这些解决方案总是无法将输入作为流来处理。在
如果您不想处理
^{pr2}$sys.stdin
,可以这样做:相关问题 更多 >
编程相关推荐