在Python中,对文件内容进行排序通常涉及以下步骤:
1. 打开文件并读取内容。
2. 将文件内容分割成行(如果需要)。
3. 对行进行排序。
4. 将排序后的行写入新文件或覆盖原文件。
打开文件并读取内容with open('input_file.txt', 'r') as file:lines = file.readlines()对行进行排序lines.sort()将排序后的行写入新文件with open('sorted_file.txt', 'w') as file:file.writelines(lines)
如果你需要按照每行的字符长度进行排序,可以使用`key`参数指定排序依据:
按字符长度对文件内容进行排序with open('test.log', 'r') as file:lines = file.readlines()使用lambda表达式指定排序依据为行长度lines.sort(key=lambda x: len(x))将排序后的行写入新文件with open('sorted_by_length.log', 'w') as file:file.writelines(lines)
对于更复杂的排序需求,比如统计特定目录下所有txt文件中每个domain出现的次数并按次数排序,可以使用以下代码:
import osimport re将给定目录下所有的txt文件整合到一个list中def merge_txt(path_list):file_list = []for path in path_list:for (dirpath, dirnames, filenames) in os.walk(path):for filename in filenames:if filename.endswith('.txt'):file_list.append(os.path.join(dirpath, filename))return file_list将文件列表中每个文件里面的url合并到一个listdef merge_to_domain_list(file_list):all_domain_list = []domain_pat = r'https?://[^\s]+'for file in file_list:with open(file, 'r') as f:for line in f:domains = re.findall(domain_pat, line)all_domain_list.extend(domains)return all_domain_list统计每个domain出现的次数domain_counts = {}for file in merge_txt(['data_naver', 'data_google']):for domain in merge_to_domain_list([file]):if domain in domain_counts:domain_counts[domain] += 1else:domain_counts[domain] = 1按照出现次数对domain进行排序sorted_domains = sorted(domain_counts.items(), key=lambda x: x, reverse=True)将排序结果写入文件with open('sorted_domains.txt', 'w') as f:for domain, count in sorted_domains:f.write(f'{domain}: {count}\n')
以上代码展示了如何读取多个目录下的txt文件,提取domain,统计它们出现的次数,并按次数降序排序,最后将结果写入文件。

