Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/316.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何按数字分解pdf文本_Python_Regex_Pdf - Fatal编程技术网

Python 如何按数字分解pdf文本

Python 如何按数字分解pdf文本,python,regex,pdf,Python,Regex,Pdf,所以我的问题不是关于pdf提取。 假设这是一个pdf文本提取 (a) 这是我的第一段,是一些垃圾文本 (b) 这是另一段,但顺便提及另一段,该段提及第945(d)条 (c) 这又是第三段 现在,我尝试创建一个包含3个值的列表,每个值代表一个段落 重新导入 整段文字=”“(a)这是我的第一段,是一些垃圾文字 (b) 这是另一段,但顺便提一下,它提到了另一段,该段提到了本文本中某个地方的第945(d)条 (c) 这又是第三段了 PDF_SUB_SECTIONS=[“(a)”、“(b)”、“(c)”、

所以我的问题不是关于pdf提取。 假设这是一个pdf文本提取

(a) 这是我的第一段,是一些垃圾文本

(b) 这是另一段,但顺便提及另一段,该段提及第945(d)条

(c) 这又是第三段

现在,我尝试创建一个包含3个值的列表,每个值代表一个段落

重新导入
整段文字=”“(a)这是我的第一段,是一些垃圾文字
(b) 这是另一段,但顺便提一下,它提到了另一段,该段提到了本文本中某个地方的第945(d)条
(c) 这又是第三段了
PDF_SUB_SECTIONS=[“(a)”、“(b)”、“(c)”、“(d)”、“(e)”、“(f)”、“(g)”]
regexpatern='|'.join(映射(关于转义,PDF子部分))
glSubSections=re.split(regexpatern,整个文本)
我所期望的是 [“这是我的第一段,是一些垃圾文本”, “这是另一段,但顺便提一下,它提到了另一段,该段提到了本文本中某个地方的第945(d)条”, “这又是第三段了”]

我得到的是 [“这是我的第一段,是一些垃圾文本”, “这是另一段,但顺便提一下,它提到了提及第945条的另一段”, “本文中的某个地方”, “这又是第三段了”]

更多信息: 1) 条例草案第945(d)条—该等“945”(或任何文本)与“(d)”之间,绝不会有任何差距 2) 我正在使用PyPDF2提取上面的文本 重新拆分(模式、完整文本、标志=重新多行) 这将起作用,但结果列表的第一个元素将是空字符串。它比其他解决方案简单一点。我们将行的开头与
^
匹配,但为了在跨多行的字符串中工作,必须将
re.MULTILINE
标志传递给
re.split
。如果您想省略错误的第一个元素,只需在结果列表中使用一个片段,如so
re.split(pattern,thigh_text,flags=re.MULTILINE)[1://code>


有关
re.MULTILINE
的更多信息,请参见

使用正则表达式有几种方法,但通常会变得更复杂,可能不是最好的方法。例如,表达式类似于:

^(?:\([^)]+\))\s*(.*)
使用
re.findall进行测试
输出
使用
re.sub进行测试
使用
re.finditer进行测试
如果您希望探索/简化/修改该表达式,将在的右上面板中对其进行解释。在中,您可以逐步观察它如何与一些示例输入匹配(如果您愿意)

正则表达式电路 可视化正则表达式:


我看不出你的期望和你得到的有什么区别。哦,是不是
945(d)
变成了
945
import re

regex = r"^(?:\([^)]+\))\s*(.*)"

test_str = ("(a) This is my first paragraph, which is some junk text\n\n"
    "(b) This is another paragraph, but it incidentally has some reference to another paragraph which refers to clause 945(d)\n\n"
    "(c) This again is is some third paragraph")

print(re.findall(regex, test_str, re.MULTILINE))
['This is my first paragraph, which is some junk text', 'This is another paragraph, but it incidentally has some reference to another paragraph which refers to clause 945(d)', 'This again is is some third paragraph']
import re

regex = r"^(?:\([^)]+\))\s*(.*)"

test_str = ("(a) This is my first paragraph, which is some junk text\n\n"
    "(b) This is another paragraph, but it incidentally has some reference to another paragraph which refers to clause 945(d)\n\n"
    "(c) This again is is some third paragraph")

subst = "\\1"

print(re.sub(regex, subst, test_str, 0, re.MULTILINE))
import re

regex = r"^(?:\([^)]+\))\s*(.*)"

test_str = ("(a) This is my first paragraph, which is some junk text\n\n"
    "(b) This is another paragraph, but it incidentally has some reference to another paragraph which refers to clause 945(d)\n\n"
    "(c) This again is is some third paragraph")

matches = re.finditer(regex, test_str, re.MULTILINE)

for matchNum, match in enumerate(matches, start=1):

    print ("Match {matchNum} was found at {start}-{end}: {match}".format(matchNum = matchNum, start = match.start(), end = match.end(), match = match.group()))

    for groupNum in range(0, len(match.groups())):
        groupNum = groupNum + 1

        print ("Group {groupNum} found at {start}-{end}: {group}".format(groupNum = groupNum, start = match.start(groupNum), end = match.end(groupNum), group = match.group(groupNum)))