如何拆分文件

2024-09-28 01:33:47 发布

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

我有以下格式的文本文件:

10900   PART1   3211034
10900   PART2   3400458
10900   PART4   3183857
10900   PART3   4152115
10900   PART5   3366650
10900   PART6   1548868
10920   PART3   4154075
10920   PART2   3404018
10920   PART1   3207571
10920   PART4   3178505
10920   PART6   1882924
10920   PART5   3363267
10940   PART6   2183534
10940   PART3   4153924
10940   PART4   3178554
10940   PART1   3207436
10940   PART5   3363585
10940   PART2   3404220

我想分割文件-首先,按第一列;第二,第三列的总和不超过10000000。在

以下是我根据第一列拆分文件的代码:

^{pr2}$

输出如我所愿:

^{3}$

我需要在每个文件的第一列和第三列的总和(3211034+3400458+3183857)不大于10000000,以此类推,对于其他文件。。。。。。。。。。。。。在


Tags: 文件代码格式文本文件part2part1总和part3
3条回答

我不明白你想怎么处理第一个专栏。但是,这里有一些python遵守了对第二列和的限制

文件ID=itertools.count(一) 以open('path/to/file')作为填充: 总和=0 阈值=10000000 outfile=open(“文件%d”%fileID,“w”)

for line in infile:
    val = int(line.strip().split()[-1])
    if threshold-sum >= val:
        outfile.write(line)
    else:
        outfile.close()
        sum = 0
        outfile = open("file%d"%next(fileID), 'w')
        outfile.write(line)

    sum += val

outfile.close()

希望这有帮助

{1>在这里使用

awk '{ s+=$3 } s>=10000000 || $1!=x { s=$3; c++ } { print > "File" c; x=$1 }' file

这将创建7个文件。以下是grep . File*的输出,显示了这些文件中的每一个的内容:

^{pr2}$

如果我没有弄错你的说明书,下面的内容可能对你有用。基本上,它检查第二个字段是否大于1000,如果大于1000,则将其打印到filecc是计数器),然后重置第二个字段的总和并增加文件计数器,等等

awk 'BEGIN {c=1}
     $3>10000000 {print $0 > ("file" c) ; c++ ; sum=0 } 
     $3< 10000000 {print $0 > ("file" c) ; sum+=$3 ; if (sum> 10000000) {sum=0;c++}}' INPUTFILE

如果要在第一列上拆分第三列的和:

^{pr2}$

是的,我知道这可以缩短。。。在

相关问题 更多 >

    热门问题