使用python 2.7和正则表达式解析日期

时间:2017-11-02 15:58:22

标签: regex python-2.7

我正在将文本转换为JSON文件,它可以正常工作,但不适用于所有文本....一些文本格式在Date之前包含一个额外的行。出于这个原因,我想要一些代码将跳过以"开头的任何信息("在日期之前。这是我的代码:enter image description here

import os
import json
import re

with open(_file, 'r') as _f:
        article = {}
        f = nonblank_lines(_f)
        for line in f:
            if re.search(r"(?i)\d+ of \d+ DOCUMENTS", line):
                next(f)
                article['date'] = next(f).strip()

1 个答案:

答案 0 :(得分:1)

使用if '(' in line:

if re.search(r"(?i)\d+ of \d+ DOCUMENTS", line):
    next(f)
    line = next(f)
    if '(' in line:
        line = next(f)
    article['date'] = line.strip()