您可以使用以下代码来实现此功能:

import random

# 读取文件
with open('test.txt', 'r') as file:
    lines = file.readlines()

# 随机删除50行
random_lines = random.sample(lines, 50)
for line in random_lines:
    lines.remove(line)

# 输出剩下的950行到res.txt
with open('res.txt', 'w') as file:
    file.writelines(lines)

请确保test.txt文件位于当前工作目录下,并且res.txt文件会在运行代码后创建或覆盖。

实现如下功能从文件testtxt读入1000行100列的数据并随机删除50行将剩下的950行输出到restxt里

原文地址: https://www.cveoy.top/t/topic/hKDO 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录