Shiyimiao
shiyimiao
2021-06-16 18:59
采纳率: 66.7%
浏览 117

用Python批量实现文件夹下所有txt行数据的分段排序

烦请各位老师支持!感谢

1、文件夹下有多个TXT文件,文件内容全部为整数(共有7列,数字间以逗号相隔);

2、删除每行后两列数字相等的行

3、通过Python实现每行前5列按从小到大排序,后两列也从小到大排序(每行前5列数字不重复,后两列数字相等的已删除);

4、排列完成后,将重新排列的TXT也存在原文件夹下,原TXT删除。

 

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • weixin_42071869
    小瘦猴 2021-06-17 16:10
    已采纳
    import os
    def show_files(path, all_files):
        file_list = os.listdir(path)
        for file in file_list:
            cur_path = os.path.join(path,file)
            if os.path.isdir(cur_path):
                show_files(cur_path, all_files)
            else:
                all_files.append(file)
        return all_files
    contents = show_files(r'D:/test/', [])
    # 循环打印show_files函数返回的文件名列表
    
    for i in contents:
        n = 0
        n2 = 0
        b=[]
        d1=[]
        list1 = []
        with open(r'D:/test/'+i,encoding='utf-8') as f:
            data = f.read().splitlines()
            for ll in range(len(data)):
                list1.append(data[ll].split(","))
            for j in list1:
                for k in j:
                    if k != '':
                        d1.append(int(k))
            for l in range(int(len(d1)/7)):
                n = n + 7
                #给每行的前5位排序
                line_q5 = d1[n2:n][0:5]
                line_q5.sort()
                #给每行的后2位排序
                line_h2 = d1[n2:n][-2:]
                line_h2.sort()
                if d1[n2:n][5] != d1[n2:n][6]:  #后两位相等则删除此行
                    b.append(line_q5+line_h2)
                n2 = n2 + 7
    
        with open(r'D:/test/' + i,"w", encoding='utf-8') as f:
            for b2 in b:
                for value in b2:
                    f.write(str(value)+',')
                f.write('\n')
    

    点赞 评论
  • m0_59109952
    m0_59109952 2021-06-16 20:25

    import os
    name=['a']
    for i in name:
    	b=[]
    	c=[]
    	with open('{0}.txt'.format(i),'r',encoding='utf-8') as f:
    		for line in f:
    			alist=line.split(',')
    			anewlist=[int(x.strip()) for x in alist]
    			if anewlist[-1]==anewlist[-2]:
    				continue
    			else:
    				b.append(anewlist)
    	with open('{0}new.txt'.format(i),'w',encoding='utf-8') as f:
    		for n in b:
    			d=sorted(n[:5])+sorted(n[5:])
    			for m in d:
    				f.write(str(m)+',')
    			f.write('\n')
    	os.remove('{0}.txt'.format(i))
    点赞 评论

相关推荐