本程序使用Python读取文本文件'data.txt'中的整数,按降序排序后写入新文件'data_asc.txt'。

# 读取整数列表
with open('data.txt', 'r') as file:
    integers = [int(line.strip()) for line in file]

# 对整数列表进行降序排序
integers.sort(reverse=True)

# 将排序后的整数列表写入data_asc.txt文件
with open('data_asc.txt', 'w') as file:
    for num in integers:
        file.write(str(num) + '
')

print('整数已按降序排序并写入data_asc.txt文件中。')

请确保在运行程序之前,'data.txt'文件已存在且包含整数,程序将在同一目录下生成'data_asc.txt'文件,其中包含按降序排序的整数。

Python排序文本文件整数:降序排序data.txt到data_asc.txt

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

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