【问题标题】:How to split a file containing many records into smaller groups of records [closed]如何将包含许多记录的文件拆分为较小的记录组[关闭]
【发布时间】:2020-03-26 15:41:09
【问题描述】:

如何将包含许多记录的文件拆分为更小的文件,每个文件包含更少的记录?

<TAG>
Record_1
</TAG>
<TAG>
Record_2
</TAG>
<TAG>
Record_3
</TAG>
<TAG>
Record_4
</TAG>
<TAG>
Record_5
</TAG>

当然,我们谈论的是非常大的数字。 目标是将这个文件分割成更小的文件,但不是每个文件只有一条记录,就像我们经常看到的使用csplit 一样。例如,这里我们希望每个文件有 2 条记录(但可能是 100 条或更多)。

所以预期的结果是:

split1

<TAG>
Record_1
</TAG>
<TAG>
Record_2
</TAG>

split2

<TAG>
Record_3
</TAG>
<TAG>
Record_4
</TAG>

split3

<TAG>
Record_5
</TAG>

如果不能使用标准命令行,我可能会考虑写几行python,但我不熟悉。 这个非常简单的任务有简单的解决方案吗?

【问题讨论】:

  • 打开文件,读取10个标签,将10个标签写入新文件,迭代直到完成。确切的问题/问题是什么?
  • 我不熟悉python,所以如果它是最好/唯一的选择,我会喜欢一个例子:)
  • 问“我从哪里开始?”的问题通常过于宽泛,不适合本网站。人们有自己解决问题的方法,因此不可能有正确的答案。仔细阅读 Where to Startedit 您的帖子。 SO 不会为您的问题提供工作代码,这不是 SO 的工作方式。见How to Ask,阅读教程,尽力而为。如果您对您的 代码有特定问题,请发布minimal reproducible example 并附上特定的可回答问题。
  • 嗯,问题很清楚,预期的结果也是,我不太了解允许这样做的 bash 命令(如果有的话),所以我问社区。公认的方法是:尽可能简单,如果需要,使用几行代码。所以对我来说一切都很清楚。
  • Stack Overflow 上有一类关于小型文本处理问题的问题,要求使用任何“标准”支持的脚本语言解决此问题Linux 实用程序。如果此类问题的答案具有简短(最好是单行)解决方案,则该问题将广受欢迎。但是,如果没有简短的解决方案,那么同时针对多种语言提出的问题的价值就相当了。不幸的是,在发布“不错”的答案之前,很难对问题进行分类。但我觉得给定的问题可能有一个很好的答案。

标签: python linux bash split


【解决方案1】:

好吧,我不得不自己玩 python。 这是一个简单且可重复使用的解决方案(当然可以改进,但效果很好)。

#!/usr/bin/python3
import sys, re, argparse

# config
parser = argparse.ArgumentParser(description='Split file into smaller ones, each one containing N blocs delimited by regexp')
parser.add_argument('inputf', metavar='FILE', type=argparse.FileType('r'),
                    help='the input file to split')
parser.add_argument('regexp', metavar='REGEXP',
                    help='the regular expression matching start of a new bloc')
parser.add_argument('--repeat', type=int, default=1, metavar='N',
                    help='the number of blocs to add per file')
parser.add_argument('--prefix', default='split_',
                    help='the prefix of generated files')
parser.add_argument('--suffix', default='.txt',
                    help='the suffix of generated files')
args = parser.parse_args()

# function
def split_file(fdin, regexp, repeat=1, prefix='split_', suffix='.txt'):
    nFile=0
    nMatch=0

    # Read file line by line
    for i, line in enumerate(fdin):
        # Check if regexp match
        if re.match(regexp, line):
            nMatch+=1

        # Increase file suffix
        if ( nMatch >= repeat ):
            nFile+=1
            nMatch=0

        # Write lines to file
        with open(f"{prefix}{nFile:03}{suffix}", "a") as fdout:
            fdout.write(line)

# run
split_file(args.inputf, args.regexp, args.repeat, args.prefix, args.suffix)

argparse 所述,用法非常简单。

./split.py -h
usage: split.py [-h] [--repeat REPEAT] [--prefix PREFIX] [--suffix SUFFIX]
                FILE REGEXP

Split file into smaller ones, each one containing N blocs delimited by regexp

positional arguments:
  FILE             the input file to split
  REGEXP           the regular expression matching start of a new bloc

optional arguments:
  -h, --help       show this help message and exit
  --repeat N       the number of blocs to add per file
  --prefix PREFIX  the prefix of generated files
  --suffix SUFFIX  the suffix of generated files

所以给定示例的答案是: ./split.py input.xml '&lt;TAG&gt;' --repeat 2

请注意,如果提供- 代替文件,则输入文件也可以是标准输入。

如果你找到了,那就尽情享受吧!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-12-09
    • 2020-09-13
    • 2019-01-19
    • 2017-10-09
    • 1970-01-01
    相关资源
    最近更新 更多