0

在 python 中实现这个 awk 命令的最佳 Pythonic 方式是什么?

awk 'BEGIN{chunk=0} /^From /{msgs++;if(msgs==500){msgs=0;chunk++}}{print > "chunk_" chunk ".txt"}' mbox

我现在用它来分割巨大的邮箱(mbox 格式)文件。

我现在正在尝试一种递归方法。

def chunkUp(mbox, chunk=0):
    with open(mbox, 'r') as bigfile:
        msg = 0
        for line in bigfile:
            if msg == 0: 
                with open("./TestChunks/chunks/chunk_"+str(chunk)+".txt", "a+") as cf:
                    if line.startswith("From "): msg += 1
                    cf.write(line)
                    if msg > 20: chunkUp(mbox, chunk+1)

我希望能够在 python 中实现这一点,并且如果它被中断能够恢复进度。现在正在处理那一点。

我正在把我的大脑打成结!干杯!

4

1 回答 1

0

您的递归方法注定要失败:您最终可能会同时打开太多文件,因为这些with块直到程序结束才会退出。

最好打开一个句柄并写入,在遇到“From”时关闭并重新打开新句柄。

还以写入模式打开文件,而不是追加。下面的代码尝试执行最少的操作和测试来将每一行写入文件中,并在From:找到时关闭/打开另一个文件。此外,最后,最后一个文件被关闭。

def chunkUp(mbox):
    with open(mbox, 'r') as bigfile:
        handle = None
        chunk = 0

        for line in bigfile:
            if line.startswith("From "):
                 # next (or first) file
                 chunk += 1
                 if handle is not None:
                    handle.close()
                 handle = None

            # file was closed / first file: create a new one
            if handle is None:
               handle = open("./TestChunks/chunks/chunk_{}.txt".format(chunk), "w")
            # write the line in the current file
            handle.write(line)

         if handle is not None:
             handle.close()

我还没有测试过,但它很简单,它应该可以工作。如果文件的第一行没有“From”,则之前的所有行都存储在chunk_0.txt文件中。

于 2019-03-29T22:18:03.893 回答