使用python跨多行拆分文本提取行

2024-04-28 21:41:17 发布

您现在位置:Python中文网/ 问答频道 /正文

我有以下代码:

f = open('./dat.txt', 'r')
array = []
for line in f:
    # if "1\t\"Overall evaluation" in line:
    #   words = line.split("1\t\"Overall evaluation")
    #   print words[0]
    number = int(line.split(':')[1].strip('"\n'))
    print number

它能够从我的数据中获取最后一个int,如下所示:

299 1   "Overall evaluation: 3
Invite to interview: 3
Strength or novelty of the idea (1): 4
Strength or novelty of the idea (2): 3
Strength or novelty of the idea (3): 3
Use or provision of open data (1): 4
Use or provision of open data (2): 3
""Open by default"" (1): 2
""Open by default"" (2): 3
Value proposition and potential scale (1): 4
Value proposition and potential scale (2): 2
Market opportunity and timing (1): 4
Market opportunity and timing (2): 4
Triple bottom line impact (1): 4
Triple bottom line impact (2): 2
Triple bottom line impact (3): 2
Knowledge and skills of the team (1): 3
Knowledge and skills of the team (2): 4
Capacity to realise the idea (1): 4
Capacity to realise the idea (2): 3
Capacity to realise the idea (3): 4
Appropriateness of the budget to realise the idea: 3"
299 2   "Overall evaluation: 3
Invite to interview: 3
Strength or novelty of the idea (1): 3
Strength or novelty of the idea (2): 2
Strength or novelty of the idea (3): 4
Use or provision of open data (1): 4
Use or provision of open data (2): 3
""Open by default"" (1): 3
""Open by default"" (2): 2
Value proposition and potential scale (1): 4
Value proposition and potential scale (2): 3
Market opportunity and timing (1): 4
Market opportunity and timing (2): 3
Triple bottom line impact (1): 3
Triple bottom line impact (2): 2
Triple bottom line impact (3): 1
Knowledge and skills of the team (1): 4
Knowledge and skills of the team (2): 4
Capacity to realise the idea (1): 4
Capacity to realise the idea (2): 4
Capacity to realise the idea (3): 4
Appropriateness of the budget to realise the idea: 2"

364 1   "Overall evaluation: 3
Invite to interview: 3
...

我还需要获取“record identifier”,在上面的示例中,前两个实例是299,下一个实例是364。你知道吗

上面注释掉的代码,如果我删除最后一行并使用它,如下所示:

f = open('./dat.txt', 'r')
array = []
for line in f:
    if "1\t\"Overall evaluation" in line:
        words = line.split("1\t\"Overall evaluation")
        print words[0]
    # number = int(line.split(':')[1].strip('"\n'))
    # print number

可以获取记录标识符。你知道吗

但我很难把两者结合起来。你知道吗

理想情况下,我想要的是以下内容:

368

=2+3+3+3+4+3+2+3+2+3+2+3+2+3+2+3+2+4+3+2+3+2

=2+3+3+3+4+3+2+3+2+3+2+3+2+3+2+3+2+4+3+2+3+2

所有记录都是如此。你知道吗

如何结合上述两个脚本组件来实现这一点?你知道吗


Tags: orandofthetolineopenstrength
1条回答
网友
1楼 · 发布于 2024-04-28 21:41:17

正则表达式就是门票。你可以用两种模式来做。像这样:

import re

with open('./dat.txt') as fin:
    for line in fin:
        ma = re.match(r'^(\d+) \d.+Overall evaluation', line)
        if ma:
            print("record identifier %r" % ma.group(1))
            continue
        ma = re.search(r': (\d+)$', line)
        if ma:
            print(ma.group(1))
            continue
        print("unrecognized line: %s" % line)

注意:最后一个print语句不是您需求的一部分,但是每当我调试regex时,我总是添加一些catchall来帮助调试糟糕的regex语句。一旦我弄清楚了我的模式,我就去掉了catchall。你知道吗

相关问题 更多 >