python日常工作处理-文件按比例分割数据
python日常工作处理-文件按比例分割数据
把一个保存用户id文本进行比例分割,比例为50%,分别另存为另外两个文件
代码
import random
input_file = '/Users/Desktop/2023-03-28.txt'
group1_file = '/Users/Desktop/group1_2023-03-28.txt'
group2_file = '/Users/Desktop/group2_2023-03-28.txt'
if __name__ == '__main__':
output_files = [
open(group1_file, 'w'),
open(group2_file, 'w')
]
counts = [0, 0]
with open(input_file, 'r') as input:
total_count = 0
for _ in input:
total_count += 1
print(f'{input_file} size:{total_count}')
total_count //= 2
input.seek(0)
for line in input:
index = random.randint(0, 1)
if counts[index] >= total_count:
index = 1 - index
output_files[index].write(line)
counts[index] += 1
print(f'{group1_file} size:{counts[0]}')
print(f'{group2_file} size:{counts[1]}')
output_files[0].close()
output_files[1].close()