在Python中按照特定模式拆分字符串文本

问题描述 投票:0回答:2

我正在处理一些遵循特定模式的文本(这是一个目录),我正在尝试提取。例如,

rawtext = 'TABLE OF CONTENTS 1 TRANSACTION OVERVIEW 10 1.1 Structure diagram 10 1.2 Risk factors 10 1.3 Principal parties 11 1.4 Notes 12 1.5 Credit structure 18 1.6 Portfolio information 19 1.7 Portfolio documentation 23 1.8 General 29 2 RISK FACTORS 31 '

该文本遵循一个特定的模式,即:(部分编号)然后(部分名称)和最后(页码)。

我对正则表达式不是很了解,但是拼凑了一些检查来提取并将这些变量放在数据帧中。

这适用于提取Section Name和Section Page(虽然我确定它可以改进)但是我无法使用这种方法识别Section Number,因为我们可以有两个整数(例如'2'代表'风险因素'部分),小数(例如'结构图'部分的'1.1'),或者根本没有(例如'目录'文本之前没有部分编号)。

我认为更有效的方法是将所有内容传递给python函数(re.match?re.findall?)并根据模式本身提取所有内容,即NUMBERS OR DECIMALS(IF PRESENT); (字母之间的字母和空格); NUMBERS

所以这意味着输出如下:

import pandas as pd
import re
import numpy as np
toc = pd.DataFrame()
toc['SectionName'] = re.findall(r'[A-Za-z-]+[ ]+[A-Za-z]*[ ]*[A-Za-z]*[ ]*[A-Za-z]*[ ]*[A-Za-z]*[ ]*[A-Za-z]*[ ]*', rawtext) # get the section names
toc['SectionPage'] = re.findall(r'[ ]+[0-9]*[ ]+', rawtext) # get the page numbers
toc.loc[1,'SectionNum'] = np.nan
toc.loc[1,'SectionNum'] = 1
toc.loc[2,'SectionNum'] = 1.1
toc.loc[3,'SectionNum'] = 1.2
toc.loc[4,'SectionNum'] = 1.3
toc.loc[5,'SectionNum'] = 1.4
toc.loc[6,'SectionNum'] = 1.5
toc.loc[7,'SectionNum'] = 1.6
toc.loc[8,'SectionNum'] = 1.7
toc.loc[9,'SectionNum'] = 1.8
toc.loc[10,'SectionNum'] = 2

toc = toc[['SectionNum', 'SectionName', 'SectionPage']]
print(toc)

我真的无法管理这个;我已经尝试了几天,并尝试搜索Stack Overflow,但没有运气(如果我错过了在其他地方发布的明显答案,请道歉)。有人会有任何想法甚至建议进一步走向解决方案吗?

非常感谢你提前!

python regex python-3.x
2个回答
0
投票

这是我到目前为止所拥有的:

import re
rawtext = 'TABLE OF CONTENTS 1 TRANSACTION OVERVIEW 10 1.1 Structure diagram 10 1.2 Risk factors 10 1.3 Principal parties 11 1.4 Notes 12 1.5 Credit structure 18 1.6 Portfolio information 19 1.7 Portfolio documentation 23 1.8 General 29 2 RISK FACTORS 31 '
print(rawtext)
matches = re.finditer(r'(\d+(?:\.\d+)?)\s+(\D*?)\s+(\d+)', rawtext)
for m in matches:
   print((m[1], m[2], m[3]))

# output
# TABLE OF CONTENTS 1 TRANSACTION OVERVIEW 10 1.1 Structure diagram 10 1.2 Risk factors 10 1.3 Principal parties 11 1.4 Notes 12 1.5 Credit structure 18 1.6 Portfolio information 19 1.7 Portfolio documentation 23 1.8 General 29 2 RISK FACTORS 31
# ('1', 'TRANSACTION OVERVIEW', '10')
# ('1.1', 'Structure diagram', '10')
# ('1.2', 'Risk factors', '10')
# ('1.3', 'Principal parties', '11')
# ('1.4', 'Notes', '12')
# ('1.5', 'Credit structure', '18')
# ('1.6', 'Portfolio information', '19')
# ('1.7', 'Portfolio documentation', '23')
# ('1.8', 'General', '29')
# ('2', 'RISK FACTORS', '31')

我刚注意到你的编辑。让我看看这是否能回答你的问题,我会对这个答案进行任何修改。

编辑:好的,我认为这回答了大部分问题,至少从我解释的内容来看。现在,这只是一个组织数据的问题,无论你认为合适。 m[1]是节号,m[2]是节名,m[3]是页码。

编辑:另外,为了解释正则表达式模式,它基本上分为3部分:

  1. (\d+(?:\.\d+)?)捕获段号,可以是整数或十进制数
  2. (\D*?)捕获0个或更多非数字非贪婪
  3. (\d+)捕获页码

编辑:在上面的1-3解释中有一个拼写错误。请注意(1)?末尾的(?:\.\d+)?。它表示匹配0或1,换句话说,是可选的浮点值


0
投票
rawtext = 'TABLE OF CONTENTS 1 TRANSACTION OVERVIEW 10 1.1 Structure diagram 10 1.2 Risk factors 10 1.3 Principal parties 11 1.4 Notes 12 1.5 Credit structure 18 1.6 Portfolio information 19 1.7 Portfolio documentation 23 1.8 General 29 2 RISK FACTORS 31 '

title = "TABLE OF CONTENTS"

text = rawtext[20:]
wordList = text.split()

indexList = []
lessonList = []
pageList= []
lessonBlank = []
for element in wordList:

    if lessonBlank == []:
        lessonBlank.append(element)
        indexList.append(element)

    else:

        try:
            temp = float(element)

            pageList.append(int(element))
            lessonBlank = []

        except ValueError as e:

            lessonBlank.append(element)
            lessonList[-1] = lessonList[-1] + " " + element
© www.soinside.com 2019 - 2024. All rights reserved.