如何连续读取细节块和计算文本行

时间:2017-05-09 03:50:15

标签: python python-2.7

是否可以在此代码中添加while循环?所以它可以不断获取行块,直到它仍能看到开始和结束字。

ArrayList

如果你要从我的文本文件中建立它,我需要从VIDEO字符串开始直到AUDIO字符串。之后从AUDIO到MKV。它应该一直持续到文件结束。这是我的文件样本。

with open('test.txt') as input_data:
    for line in input_data:
        if line.strip() == 'Start': 
            break
    for line in input_data: 
        if line.strip() == 'End':
            break
        print line

实际文件比这个文件长。 获取行块后,我想计算具有数据的实例数,例如百分比。就像从视频到音频的第一个块一样,它有17行。

示例输出

*****START OF YOUTUBE*****
VIDEO

[download]   0.0% of 5.30MiB at 188.75KiB/s ETA 00:29
[download]   0.1% of 5.30MiB at 474.99KiB/s ETA 00:11
[download]   0.1% of 5.30MiB at 984.97KiB/s ETA 00:05
[download]   0.3% of 5.30MiB at  1.85MiB/s ETA 00:02
[download]   0.6% of 5.30MiB at 896.97KiB/s ETA 00:06
[download]   1.2% of 5.30MiB at 888.54KiB/s ETA 00:06
[download]   2.3% of 5.30MiB at 1006.45KiB/s ETA 00:05
[download]   4.7% of 5.30MiB at 916.41KiB/s ETA 00:05
[download]   9.4% of 5.30MiB at 863.70KiB/s ETA 00:05
[download]  18.9% of 5.30MiB at 847.73KiB/s ETA 00:05
[download]  34.2% of 5.30MiB at 843.72KiB/s ETA 00:04
[download]  49.7% of 5.30MiB at 840.54KiB/s ETA 00:03
[download]  65.0% of 5.30MiB at 839.77KiB/s ETA 00:02
[download]  80.5% of 5.30MiB at 839.71KiB/s ETA 00:01
[download]  95.9% of 5.30MiB at 839.39KiB/s ETA 00:00
[download] 100.0% of 5.30MiB at 842.74KiB/s ETA 00:00
[download] 100% of 5.30MiB in 00:06
AUDIO

[download]   0.0% of 2.99MiB at 112.61KiB/s ETA 00:27
[download]   0.1% of 2.99MiB at 311.43KiB/s ETA 00:09
[download]   0.2% of 2.99MiB at 682.19KiB/s ETA 00:04
[download]   0.5% of 2.99MiB at  1.32MiB/s ETA 00:02
[download]   1.0% of 2.99MiB at 943.80KiB/s ETA 00:03
[download]   2.1% of 2.99MiB at  1.17MiB/s ETA 00:02
[download]   4.1% of 2.99MiB at  1.31MiB/s ETA 00:02
[download]   8.3% of 2.99MiB at  1.59MiB/s ETA 00:01
[download]  16.7% of 2.99MiB at  1.37MiB/s ETA 00:01
[download]  33.4% of 2.99MiB at  1.03MiB/s ETA 00:01
[download]  60.8% of 2.99MiB at 942.47KiB/s ETA 00:01
[download]  88.1% of 2.99MiB at 908.52KiB/s ETA 00:00
[download] 100.0% of 2.99MiB at 897.53KiB/s ETA 00:00
[download] 100% of 2.99MiB in 00:03
MKV

05-05-2017_00:35:15:677036
*****END OF YOUTUBE*****
*****START OF YOUTUBE*****

VIDEO

[download]   0.0% of 41.89MiB at 251.26KiB/s ETA 02:55
[download]   0.0% of 41.89MiB at 612.01KiB/s ETA 01:10
[download]   0.0% of 41.89MiB at  1.23MiB/s ETA 00:34
[download]   0.0% of 41.89MiB at  2.33MiB/s ETA 00:18
[download]   0.1% of 41.89MiB at 783.21KiB/s ETA 00:54
[download]   0.1% of 41.89MiB at 959.46KiB/s ETA 00:44
[download]   0.3% of 41.89MiB at  1.16MiB/s ETA 00:36
[download]   0.6% of 41.89MiB at  1.45MiB/s ETA 00:28
[download]   1.2% of 41.89MiB at  1.15MiB/s ETA 00:35
[download]   2.4% of 41.89MiB at 986.82KiB/s ETA 00:42
[download]   4.4% of 41.89MiB at 912.91KiB/s ETA 00:44
[download]   6.3% of 41.89MiB at 877.92KiB/s ETA 00:45
[download]   8.2% of 41.89MiB at 870.89KiB/s ETA 00:45
[download]  10.2% of 41.89MiB at 865.95KiB/s ETA 00:44
[download]  12.1% of 41.89MiB at 862.33KiB/s ETA 00:43
[download]  14.1% of 41.89MiB at 859.01KiB/s ETA 00:42
[download]  16.1% of 41.89MiB at 857.18KiB/s ETA 00:42
[download]  18.0% of 41.89MiB at 855.34KiB/s ETA 00:41
[download]  20.0% of 41.89MiB at 850.76KiB/s ETA 00:40
[download]  21.9% of 41.89MiB at 850.71KiB/s ETA 00:39
[download]  23.9% of 41.89MiB at 850.61KiB/s ETA 00:38
[download]  25.9% of 41.89MiB at 850.12KiB/s ETA 00:37
[download]  27.8% of 41.89MiB at 849.74KiB/s ETA 00:36
[download]  29.8% of 41.89MiB at 849.08KiB/s ETA 00:35
[download]  31.7% of 41.89MiB at 848.78KiB/s ETA 00:34
[download]  33.7% of 41.89MiB at 846.31KiB/s ETA 00:33
[download]  35.6% of 41.89MiB at 844.42KiB/s ETA 00:32
[download]  37.5% of 41.89MiB at 844.68KiB/s ETA 00:31
[download]  39.5% of 41.89MiB at 844.87KiB/s ETA 00:30
[download]  41.5% of 41.89MiB at 844.76KiB/s ETA 00:29
[download]  43.4% of 41.89MiB at 844.93KiB/s ETA 00:28
[download]  45.4% of 41.89MiB at 844.77KiB/s ETA 00:27
[download]  47.4% of 41.89MiB at 843.37KiB/s ETA 00:26
[download]  49.2% of 41.89MiB at 843.65KiB/s ETA 00:25
[download]  51.2% of 41.89MiB at 843.66KiB/s ETA 00:24
[download]  53.2% of 41.89MiB at 843.50KiB/s ETA 00:23
[download]  55.2% of 41.89MiB at 843.46KiB/s ETA 00:22
[download]  57.1% of 41.89MiB at 843.19KiB/s ETA 00:21
[download]  59.1% of 41.89MiB at 843.22KiB/s ETA 00:20
[download]  61.0% of 41.89MiB at 842.88KiB/s ETA 00:19
[download]  63.0% of 41.89MiB at 843.00KiB/s ETA 00:18
[download]  65.0% of 41.89MiB at 842.80KiB/s ETA 00:17
[download]  66.9% of 41.89MiB at 842.91KiB/s ETA 00:16
[download]  68.9% of 41.89MiB at 842.09KiB/s ETA 00:15
[download]  70.8% of 41.89MiB at 842.40KiB/s ETA 00:14
[download]  72.8% of 41.89MiB at 840.95KiB/s ETA 00:13
[download]  74.6% of 41.89MiB at 840.38KiB/s ETA 00:12
[download]  76.5% of 41.89MiB at 840.77KiB/s ETA 00:11
[download]  78.5% of 41.89MiB at 840.21KiB/s ETA 00:10
[download]  80.4% of 41.89MiB at 840.45KiB/s ETA 00:09
[download]  82.4% of 41.89MiB at 840.65KiB/s ETA 00:08
[download]  84.4% of 41.89MiB at 840.80KiB/s ETA 00:07
[download]  86.4% of 41.89MiB at 840.35KiB/s ETA 00:06
[download]  88.3% of 41.89MiB at 840.73KiB/s ETA 00:05
[download]  90.3% of 41.89MiB at 840.32KiB/s ETA 00:04
[download]  92.2% of 41.89MiB at 840.69KiB/s ETA 00:03
[download]  94.2% of 41.89MiB at 840.31KiB/s ETA 00:02
[download]  96.1% of 41.89MiB at 840.64KiB/s ETA 00:01
[download]  98.1% of 41.89MiB at 840.26KiB/s ETA 00:00
[download] 100.0% of 41.89MiB at 840.32KiB/s ETA 00:00
[download] 100% of 41.89MiB in 00:51
AUDIO

[download]   0.0% of 10.35MiB at 114.98KiB/s ETA 01:32
[download]   0.0% of 10.35MiB at 317.99KiB/s ETA 00:33
[download]   0.1% of 10.35MiB at 694.85KiB/s ETA 00:15
[download]   0.1% of 10.35MiB at  1.37MiB/s ETA 00:07
[download]   0.3% of 10.35MiB at 846.04KiB/s ETA 00:12
[download]   0.6% of 10.35MiB at  1.14MiB/s ETA 00:09
[download]   1.2% of 10.35MiB at  1.29MiB/s ETA 00:07
[download]   2.4% of 10.35MiB at  1.71MiB/s ETA 00:05
[download]   4.8% of 10.35MiB at  2.00MiB/s ETA 00:04
[download]   9.7% of 10.35MiB at  1.17MiB/s ETA 00:07
[download]  17.7% of 10.35MiB at 985.70KiB/s ETA 00:08
[download]  25.3% of 10.35MiB at 940.66KiB/s ETA 00:08
[download]  33.3% of 10.35MiB at 916.90KiB/s ETA 00:07
[download]  41.4% of 10.35MiB at 900.34KiB/s ETA 00:06
[download]  49.3% of 10.35MiB at 885.27KiB/s ETA 00:06
[download]  56.9% of 10.35MiB at 880.58KiB/s ETA 00:05
[download]  65.0% of 10.35MiB at 876.17KiB/s ETA 00:04
[download]  73.0% of 10.35MiB at 868.96KiB/s ETA 00:03
[download]  80.7% of 10.35MiB at 867.56KiB/s ETA 00:02
[download]  88.7% of 10.35MiB at 865.95KiB/s ETA 00:01
[download]  96.7% of 10.35MiB at 862.35KiB/s ETA 00:00
[download] 100.0% of 10.35MiB at 862.21KiB/s ETA 00:00
[download] 100% of 10.35MiB in 00:12
MKV


05-05-2017_00:36:24:569523
*****END OF YOUTUBE*****
*****START OF YOUTUBE*****

VIDEO

[download]   0.0% of 5.30MiB at 188.75KiB/s ETA 00:29
[download]   0.1% of 5.30MiB at 474.99KiB/s ETA 00:11
[download]   0.1% of 5.30MiB at 984.97KiB/s ETA 00:05
[download]   0.3% of 5.30MiB at  1.85MiB/s ETA 00:02
[download]   0.6% of 5.30MiB at 896.97KiB/s ETA 00:06
[download]   1.2% of 5.30MiB at 888.54KiB/s ETA 00:06
[download]   2.3% of 5.30MiB at 1006.45KiB/s ETA 00:05
[download]   4.7% of 5.30MiB at 916.41KiB/s ETA 00:05
[download]   9.4% of 5.30MiB at 863.70KiB/s ETA 00:05
[download]  18.9% of 5.30MiB at 847.73KiB/s ETA 00:05
[download]  34.2% of 5.30MiB at 843.72KiB/s ETA 00:04
[download]  49.7% of 5.30MiB at 840.54KiB/s ETA 00:03
[download]  65.0% of 5.30MiB at 839.77KiB/s ETA 00:02
[download]  80.5% of 5.30MiB at 839.71KiB/s ETA 00:01
[download]  95.9% of 5.30MiB at 839.39KiB/s ETA 00:00
[download] 100.0% of 5.30MiB at 842.74KiB/s ETA 00:00
[download] 100% of 5.30MiB in 00:06
AUDIO

[download]   0.0% of 2.99MiB at 112.61KiB/s ETA 00:27
[download]   0.1% of 2.99MiB at 311.43KiB/s ETA 00:09
[download]   0.2% of 2.99MiB at 682.19KiB/s ETA 00:04
[download]   0.5% of 2.99MiB at  1.32MiB/s ETA 00:02
[download]   1.0% of 2.99MiB at 943.80KiB/s ETA 00:03
[download]   2.1% of 2.99MiB at  1.17MiB/s ETA 00:02
[download]   4.1% of 2.99MiB at  1.31MiB/s ETA 00:02
[download]   8.3% of 2.99MiB at  1.59MiB/s ETA 00:01
[download]  16.7% of 2.99MiB at  1.37MiB/s ETA 00:01
[download]  33.4% of 2.99MiB at  1.03MiB/s ETA 00:01
[download]  60.8% of 2.99MiB at 942.47KiB/s ETA 00:01
[download]  88.1% of 2.99MiB at 908.52KiB/s ETA 00:00
[download] 100.0% of 2.99MiB at 897.53KiB/s ETA 00:00
[download] 100% of 2.99MiB in 00:03
MKV


05-05-2017_00:35:15:677036
*****END OF YOUTUBE*****

然后它再次重复,直到文件结束,所以它应该是

最终样本输出

Video to Audio
Count = 17

Audio to MKV
Count = 14

3 个答案:

答案 0 :(得分:1)

以下是使用字符串find方法的解决方案:

您给出的字符串作为示例:

string = """
*****START OF YOUTUBE*****

VIDEO

[download]   0.0% of 5.30MiB at 188.75KiB/s ETA 00:29
[download]   0.1% of 5.30MiB at 474.99KiB/s ETA 00:11
[download]   0.1% of 5.30MiB at 984.97KiB/s ETA 00:05
[download]   0.3% of 5.30MiB at  1.85MiB/s ETA 00:02
[download]   0.6% of 5.30MiB at 896.97KiB/s ETA 00:06
[download]   1.2% of 5.30MiB at 888.54KiB/s ETA 00:06
[download]   2.3% of 5.30MiB at 1006.45KiB/s ETA 00:05
[download]   4.7% of 5.30MiB at 916.41KiB/s ETA 00:05
[download]   9.4% of 5.30MiB at 863.70KiB/s ETA 00:05
[download]  18.9% of 5.30MiB at 847.73KiB/s ETA 00:05
[download]  34.2% of 5.30MiB at 843.72KiB/s ETA 00:04
[download]  49.7% of 5.30MiB at 840.54KiB/s ETA 00:03
[download]  65.0% of 5.30MiB at 839.77KiB/s ETA 00:02
[download]  80.5% of 5.30MiB at 839.71KiB/s ETA 00:01
[download]  95.9% of 5.30MiB at 839.39KiB/s ETA 00:00
[download] 100.0% of 5.30MiB at 842.74KiB/s ETA 00:00
[download] 100% of 5.30MiB in 00:06
AUDIO

[download]   0.0% of 2.99MiB at 112.61KiB/s ETA 00:27
[download]   0.1% of 2.99MiB at 311.43KiB/s ETA 00:09
[download]   0.2% of 2.99MiB at 682.19KiB/s ETA 00:04
[download]   0.5% of 2.99MiB at  1.32MiB/s ETA 00:02
[download]   1.0% of 2.99MiB at 943.80KiB/s ETA 00:03
[download]   2.1% of 2.99MiB at  1.17MiB/s ETA 00:02
[download]   4.1% of 2.99MiB at  1.31MiB/s ETA 00:02
[download]   8.3% of 2.99MiB at  1.59MiB/s ETA 00:01
[download]  16.7% of 2.99MiB at  1.37MiB/s ETA 00:01
[download]  33.4% of 2.99MiB at  1.03MiB/s ETA 00:01
[download]  60.8% of 2.99MiB at 942.47KiB/s ETA 00:01
[download]  88.1% of 2.99MiB at 908.52KiB/s ETA 00:00
[download] 100.0% of 2.99MiB at 897.53KiB/s ETA 00:00
[download] 100% of 2.99MiB in 00:03
MKV

05-05-2017_00:35:15:677036
*****END OF YOUTUBE*****
*****START OF YOUTUBE*****

VIDEO

[download]   0.0% of 41.89MiB at 251.26KiB/s ETA 02:55
[download]   0.0% of 41.89MiB at 612.01KiB/s ETA 01:10
[download]   0.0% of 41.89MiB at  1.23MiB/s ETA 00:34
[download]   0.0% of 41.89MiB at  2.33MiB/s ETA 00:18
[download]   0.1% of 41.89MiB at 783.21KiB/s ETA 00:54
[download]   0.1% of 41.89MiB at 959.46KiB/s ETA 00:44
[download]   0.3% of 41.89MiB at  1.16MiB/s ETA 00:36
[download]   0.6% of 41.89MiB at  1.45MiB/s ETA 00:28
[download]   1.2% of 41.89MiB at  1.15MiB/s ETA 00:35
[download]   2.4% of 41.89MiB at 986.82KiB/s ETA 00:42
[download]   4.4% of 41.89MiB at 912.91KiB/s ETA 00:44
[download]   6.3% of 41.89MiB at 877.92KiB/s ETA 00:45
[download]   8.2% of 41.89MiB at 870.89KiB/s ETA 00:45
[download]  10.2% of 41.89MiB at 865.95KiB/s ETA 00:44
[download]  12.1% of 41.89MiB at 862.33KiB/s ETA 00:43
[download]  14.1% of 41.89MiB at 859.01KiB/s ETA 00:42
[download]  16.1% of 41.89MiB at 857.18KiB/s ETA 00:42
[download]  18.0% of 41.89MiB at 855.34KiB/s ETA 00:41
[download]  20.0% of 41.89MiB at 850.76KiB/s ETA 00:40
[download]  21.9% of 41.89MiB at 850.71KiB/s ETA 00:39
[download]  23.9% of 41.89MiB at 850.61KiB/s ETA 00:38
[download]  25.9% of 41.89MiB at 850.12KiB/s ETA 00:37
[download]  27.8% of 41.89MiB at 849.74KiB/s ETA 00:36
[download]  29.8% of 41.89MiB at 849.08KiB/s ETA 00:35
[download]  31.7% of 41.89MiB at 848.78KiB/s ETA 00:34
[download]  33.7% of 41.89MiB at 846.31KiB/s ETA 00:33
[download]  35.6% of 41.89MiB at 844.42KiB/s ETA 00:32
[download]  37.5% of 41.89MiB at 844.68KiB/s ETA 00:31
[download]  39.5% of 41.89MiB at 844.87KiB/s ETA 00:30
[download]  41.5% of 41.89MiB at 844.76KiB/s ETA 00:29
[download]  43.4% of 41.89MiB at 844.93KiB/s ETA 00:28
[download]  45.4% of 41.89MiB at 844.77KiB/s ETA 00:27
[download]  47.4% of 41.89MiB at 843.37KiB/s ETA 00:26
[download]  49.2% of 41.89MiB at 843.65KiB/s ETA 00:25
[download]  51.2% of 41.89MiB at 843.66KiB/s ETA 00:24
[download]  53.2% of 41.89MiB at 843.50KiB/s ETA 00:23
[download]  55.2% of 41.89MiB at 843.46KiB/s ETA 00:22
[download]  57.1% of 41.89MiB at 843.19KiB/s ETA 00:21
[download]  59.1% of 41.89MiB at 843.22KiB/s ETA 00:20
[download]  61.0% of 41.89MiB at 842.88KiB/s ETA 00:19
[download]  63.0% of 41.89MiB at 843.00KiB/s ETA 00:18
[download]  65.0% of 41.89MiB at 842.80KiB/s ETA 00:17
[download]  66.9% of 41.89MiB at 842.91KiB/s ETA 00:16
[download]  68.9% of 41.89MiB at 842.09KiB/s ETA 00:15
[download]  70.8% of 41.89MiB at 842.40KiB/s ETA 00:14
[download]  72.8% of 41.89MiB at 840.95KiB/s ETA 00:13
[download]  74.6% of 41.89MiB at 840.38KiB/s ETA 00:12
[download]  76.5% of 41.89MiB at 840.77KiB/s ETA 00:11
[download]  78.5% of 41.89MiB at 840.21KiB/s ETA 00:10
[download]  80.4% of 41.89MiB at 840.45KiB/s ETA 00:09
[download]  82.4% of 41.89MiB at 840.65KiB/s ETA 00:08
[download]  84.4% of 41.89MiB at 840.80KiB/s ETA 00:07
[download]  86.4% of 41.89MiB at 840.35KiB/s ETA 00:06
[download]  88.3% of 41.89MiB at 840.73KiB/s ETA 00:05
[download]  90.3% of 41.89MiB at 840.32KiB/s ETA 00:04
[download]  92.2% of 41.89MiB at 840.69KiB/s ETA 00:03
[download]  94.2% of 41.89MiB at 840.31KiB/s ETA 00:02
[download]  96.1% of 41.89MiB at 840.64KiB/s ETA 00:01
[download]  98.1% of 41.89MiB at 840.26KiB/s ETA 00:00
[download] 100.0% of 41.89MiB at 840.32KiB/s ETA 00:00
[download] 100% of 41.89MiB in 00:51
AUDIO

[download]   0.0% of 10.35MiB at 114.98KiB/s ETA 01:32
[download]   0.0% of 10.35MiB at 317.99KiB/s ETA 00:33
[download]   0.1% of 10.35MiB at 694.85KiB/s ETA 00:15
[download]   0.1% of 10.35MiB at  1.37MiB/s ETA 00:07
[download]   0.3% of 10.35MiB at 846.04KiB/s ETA 00:12
[download]   0.6% of 10.35MiB at  1.14MiB/s ETA 00:09
[download]   1.2% of 10.35MiB at  1.29MiB/s ETA 00:07
[download]   2.4% of 10.35MiB at  1.71MiB/s ETA 00:05
[download]   4.8% of 10.35MiB at  2.00MiB/s ETA 00:04
[download]   9.7% of 10.35MiB at  1.17MiB/s ETA 00:07
[download]  17.7% of 10.35MiB at 985.70KiB/s ETA 00:08
[download]  25.3% of 10.35MiB at 940.66KiB/s ETA 00:08
[download]  33.3% of 10.35MiB at 916.90KiB/s ETA 00:07
[download]  41.4% of 10.35MiB at 900.34KiB/s ETA 00:06
[download]  49.3% of 10.35MiB at 885.27KiB/s ETA 00:06
[download]  56.9% of 10.35MiB at 880.58KiB/s ETA 00:05
[download]  65.0% of 10.35MiB at 876.17KiB/s ETA 00:04
[download]  73.0% of 10.35MiB at 868.96KiB/s ETA 00:03
[download]  80.7% of 10.35MiB at 867.56KiB/s ETA 00:02
[download]  88.7% of 10.35MiB at 865.95KiB/s ETA 00:01
[download]  96.7% of 10.35MiB at 862.35KiB/s ETA 00:00
[download] 100.0% of 10.35MiB at 862.21KiB/s ETA 00:00
[download] 100% of 10.35MiB in 00:12
MKV


05-05-2017_00:36:24:569523
*****END OF YOUTUBE*****
*****START OF YOUTUBE*****

VIDEO

[download]   0.0% of 5.30MiB at 188.75KiB/s ETA 00:29
[download]   0.1% of 5.30MiB at 474.99KiB/s ETA 00:11
[download]   0.1% of 5.30MiB at 984.97KiB/s ETA 00:05
[download]   0.3% of 5.30MiB at  1.85MiB/s ETA 00:02
[download]   0.6% of 5.30MiB at 896.97KiB/s ETA 00:06
[download]   1.2% of 5.30MiB at 888.54KiB/s ETA 00:06
[download]   2.3% of 5.30MiB at 1006.45KiB/s ETA 00:05
[download]   4.7% of 5.30MiB at 916.41KiB/s ETA 00:05
[download]   9.4% of 5.30MiB at 863.70KiB/s ETA 00:05
[download]  18.9% of 5.30MiB at 847.73KiB/s ETA 00:05
[download]  34.2% of 5.30MiB at 843.72KiB/s ETA 00:04
[download]  49.7% of 5.30MiB at 840.54KiB/s ETA 00:03
[download]  65.0% of 5.30MiB at 839.77KiB/s ETA 00:02
[download]  80.5% of 5.30MiB at 839.71KiB/s ETA 00:01
[download]  95.9% of 5.30MiB at 839.39KiB/s ETA 00:00
[download] 100.0% of 5.30MiB at 842.74KiB/s ETA 00:00
[download] 100% of 5.30MiB in 00:06
AUDIO

[download]   0.0% of 2.99MiB at 112.61KiB/s ETA 00:27
[download]   0.1% of 2.99MiB at 311.43KiB/s ETA 00:09
[download]   0.2% of 2.99MiB at 682.19KiB/s ETA 00:04
[download]   0.5% of 2.99MiB at  1.32MiB/s ETA 00:02
[download]   1.0% of 2.99MiB at 943.80KiB/s ETA 00:03
[download]   2.1% of 2.99MiB at  1.17MiB/s ETA 00:02
[download]   4.1% of 2.99MiB at  1.31MiB/s ETA 00:02
[download]   8.3% of 2.99MiB at  1.59MiB/s ETA 00:01
[download]  16.7% of 2.99MiB at  1.37MiB/s ETA 00:01
[download]  33.4% of 2.99MiB at  1.03MiB/s ETA 00:01
[download]  60.8% of 2.99MiB at 942.47KiB/s ETA 00:01
[download]  88.1% of 2.99MiB at 908.52KiB/s ETA 00:00
[download] 100.0% of 2.99MiB at 897.53KiB/s ETA 00:00
[download] 100% of 2.99MiB in 00:03
MKV


05-05-2017_00:35:15:677036
*****END OF YOUTUBE*****
"""

实际解决方案:

audio_indexes = []
video_indexes = []
mkv_indexes = []
audios = string.find("AUDIO")
while audios > 0:
    audio_indexes.append(audios)
    audios = string.find("AUDIO", audios+len("AUDIO"))
videos = string.find("VIDEO")
while videos > 0:
    video_indexes.append(videos)
    videos = string.find("VIDEO", videos+len("VIDEO"))
mkv = string.find("MKV")
while mkv > 0:
    mkv_indexes.append(mkv)
    mkv = string.find("MKV", mkv+len("MKV"))

for i, k in enumerate(video_indexes):
    print("Video to Audio: %d" % string[k:audio_indexes[i]].count('[download]'))
for i, k in enumerate(audio_indexes):
    print("Audio to MKV: %d" % string[k:mkv_indexes[i]].count('[download]'))

输出:

Video to Audio: 17
Video to Audio: 61
Video to Audio: 17
Audio to MKV: 14
Audio to MKV: 23
Audio to MKV: 14

当然,您可以更改这些内容的打印顺序。

答案 1 :(得分:0)

这是一个如何只处理文件一次的示例(它不使用while循环,但你可以把它变成看起来稍微丑陋的那个)

video_lines = []
audio_lines = []
mkv_lines = []

start_video_lines = False
start_audio_lines = False
start_mkv_lines = False

with open('test.txt') as input_data:
    for line in input_data:
        if line.strip() == 'VIDEO':
            start_video_lines = True
            continue
        if line.strip() == 'AUDIO':
            start_audio_lines = True
            continue
        if line.strip() == 'MKV':
            start_mkv_lines = True
            continue

        if start_mkv_lines:
            mkv_lines += line
        elif start_audio_lines:
            audio_lines += line
        elif start_video_lines:
            video_lines += line

# Then you can do your processing of mkv_lines, audio_lines, and video_lines here.

答案 2 :(得分:0)

如果您希望对数据集进行任何其他分析,我的解决方案将处理大型输入文件并提供方便的数据结构。

from __future__ import print_function


def parse_file(f):
    """
    Parse file into sections by walking the through the file and extracting counts.

    Returns a list of tuples (START_WORD, END_WORD, LINE_COUNT)

    :param f: Input file
    :returns: List of tuples
    """
    results = []
    current_block = None
    current_section = None
    line_counter = 0

    for line in f:
        if 'START' in line:
            # Start of a block create a new block and clear section
            current_block = []
            current_section = None

        elif 'END' in line:
            # End of a block, append the current one to the result list
            results.append(current_block)

        elif '[download]' in line:
            line_counter += 1

        else:
            # Handle video/audio/mkv section counts
            line = line.strip()
            if line in ('VIDEO', 'AUDIO', 'MKV'):
                if current_section:
                    current_block.append((current_section, line, line_counter))
                current_section = line
                line_counter = 0

    return results


# Input data is expected to be in *data.txt*
with open('data.txt') as f:
    results = parse_file(f)

    for block in results:
        for start, end, count in block:
            print("{} to {}\nCount = {}\n".format(start.title(), end.title(), count))