首次提交
This commit is contained in:
30
tools/split_txt.py
Normal file
30
tools/split_txt.py
Normal file
@@ -0,0 +1,30 @@
|
||||
"""
|
||||
将 txt 文件按指定行数拆分为多个子文件。
|
||||
用法:python split_txt.py [输入文件] [每份行数]
|
||||
默认:python split_txt.py text/0004_has_srt.txt 100
|
||||
"""
|
||||
import os
|
||||
import sys
|
||||
|
||||
def split_txt(input_path: str, chunk_size: int = 100):
|
||||
with open(input_path, 'r', encoding='utf-8') as f:
|
||||
lines = [line.strip() for line in f if line.strip()]
|
||||
|
||||
total = len(lines)
|
||||
num_files = (total + chunk_size - 1) // chunk_size
|
||||
out_dir = os.path.dirname(input_path)
|
||||
base_name = os.path.splitext(os.path.basename(input_path))[0]
|
||||
|
||||
for i in range(num_files):
|
||||
chunk = lines[i * chunk_size:(i + 1) * chunk_size]
|
||||
out_path = os.path.join(out_dir, f"{base_name}_{i + 1:04d}.txt")
|
||||
with open(out_path, 'w', encoding='utf-8') as f:
|
||||
f.write('\n'.join(chunk) + '\n')
|
||||
print(f" {out_path} ({len(chunk)} 条)")
|
||||
|
||||
print(f"\n共 {total} 条,拆分为 {num_files} 个文件(每份 {chunk_size} 条)")
|
||||
|
||||
if __name__ == '__main__':
|
||||
input_file = sys.argv[1] if len(sys.argv) > 1 else 'text/0004_has_srt.txt'
|
||||
chunk_size = int(sys.argv[2]) if len(sys.argv) > 2 else 100
|
||||
split_txt(input_file, chunk_size)
|
||||
Reference in New Issue
Block a user