某文件data.txt 中存放了若干个整数,各整数之间使用英文逗号分割,用python编写程序编写程序?
发布网友
发布时间:2022-04-19 03:19
我来回答
共2个回答
热心网友
时间:2022-04-19 04:48
大致如下:
# 读取文件
file = r"D:\folderpath\data.txt"
with open(file, "r") as f:
a = [i.strip("\n").split(",") for i in f.readlines()]
上述操作后将得到一个嵌套列表,如:[['1', '12'], ['1', '3'], ['5', '2']]
#展开列表,并排序 eg.[1, 1, 2, 2, 3, 4, 4]
b =sorted( [int(i) for j in a for i in j])
# output
outfile = r"d:\test\output.txt"
with open(outfile,"w") as o:
for i in b:
j = str(i) + "\n"
o.write(j)
热心网友
时间:2022-04-19 06:06
这完全是业务逻辑 你没说你遇到了什么问题 帮写私信