【问题标题】:Parsing a text file with date headings and multiple entries解析带有日期标题和多个条目的文本文件
【发布时间】:2020-01-22 09:43:14
【问题描述】:

我一直在尝试解析我拥有的大型文本文件并将其转换为字典以进行进一步分析。这是文本文件的示例:

Mar 2 (2020, year not always present)
first paragraph
second line of first paragraph

second paragraph
second line of second paragraph

Mar 3
More lines
these two should be grouped together
because they don't have a blank line in between them

however this line is a start of a new "entry"

sometimes they only have one line, sometimes many.

理想情况下,这将生成以下 Python 字典:

{"Mar 2": ["first paragraph\nsecond line of first paragraph", "second paragraph\nsecond line of second paragraph"], "Mar 3": ["More lines\nthese two should be grouped together\nbecause they\ndon't have a blank line in between them", "however this line is a start of a new \"entry\"", "sometimes they only have one line, sometimes many."]}

我已经尝试使用以下代码,它几乎可以工作,但我不确定出了什么问题。

def isdate(line):
    return line.lower().split(" ")[0] in ("jan", "feb", "mar", "apr", "may", "jun", "jul", "aug", "sept", "oct", "nov", "dec")

data = ...

lines = data.split("\n")
i = 0
data = {}
while i < len(lines):
    if isdate(lines[i]):
        date = lines[i]
        data[date] = []
        i += 1
        while not isdate(lines[i]):
            curr_d = ""
            while lines[i].strip(" ") != "":
                curr_d += lines[i] + "\n"
                i += 1
            i += 1
            data[date].append(curr_d)
    else:
        print("error in parsing")
        break

我的代码的问题在于它输出了正确的数据但是它在到达末尾时崩溃了。很抱歉之前没有包括这个,我只是意识到它实际上确实输出了正确的数据但只是崩溃了。

我不认为这是重复的,尽管很难搜索到如此具体但又如此笼统的东西(如果你明白我的意思的话),尽管我相信那里的一些 SO wizzard 会纠正我。

提前致谢。

【问题讨论】:

  • I've tried using the following code and it almost works but I'm not sure what's wrong.。您需要提供预期输出和实际输出
  • @JammyDodger 好的,我会及时处理的
  • @JammyDodger 好吧,我刚刚重新检查了我的代码,现在我意识到它可以工作,但最后只是崩溃(索引超出范围)。我应该关闭这个问题吗,因为我认为我现在应该能够自己解决它?
  • 如果您这样做,请包含修复以防其他人遇到此问题
  • 崩溃怎么办?你有回溯吗?

标签: python string dictionary parsing text


【解决方案1】:

你可以这样做:

def isdate(line):
    return line.lower().split(" ")[0] in ("jan", "feb", "mar", "apr", "may", "jun", "jul", "aug", "sept", "oct", "nov", "dec")

with open("file.txt") as f:
    lines = f.readlines()

data = {}
line_concat = ""
for line in lines:
    if isdate(line): # new key
        if line_concat: # save previous element to old data point
            data[key].append(line_concat)
        month, day = line.split(" ")[0:2]
        key = month+" "+day
        data[key] = []
        continue
    if line.strip(): # if not empty line
        line_concat += line # add line to actual value as memory
    else:
        data[key].append(line_concat) # add the element to the list
        line_concat = "" # set memory empty
data[key].append(line_concat)
for d in data:
    print(d,data[d])

输出:

Mar 2 ['first paragraph\nsecond line of first paragraph\n', 'second paragraph\nsecond line of second paragraph\n']
Mar 3
 ["More lines\nthese two should be grouped together\nbecause they don't have a blank line in between them\n", 'however this line is a start of a new "entry"\n', 'sometimes they only have one line, sometimes many.']

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-07-07
    • 1970-01-01
    • 1970-01-01
    • 2022-12-17
    • 1970-01-01
    • 2022-01-25
    • 2020-02-03
    • 1970-01-01
    相关资源
    最近更新 更多