簡體   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