繁体   English   中英

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

[英]Split string text following a specific pattern in Python

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

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 '

文本遵循特定的模式,即:(部分编号),然后是(部分名称),最后是(页面编号)。

我对正则表达式不是很好,但是已经拼凑了一些检查以提取并把这些变量放在数据框中。

这对于提取“节名”和“节页”效果很好(尽管我确定可以改进),但是由于我们可以同时使用两个整数(例如,“风险因素”部分,小数点(例如“结构图”部分为“ 1.1”)或完全没有(例如“目录”文本前没有节号)。

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

因此,这意味着输出如下:

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上进行搜索,但是没有运气(如果我错过了在其他地方发布的对此文件的明显答复,我们深表歉意)。 有人会提出任何想法或建议,以进一步寻求解决方案吗?

提前非常感谢您!

这是我到目前为止的内容:

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,换句话说,是可选的浮点值

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM