python - 获取文件中的重复行
问题描述
处理数千行的文件
试图找到准确重复的行(2次)
from collections import Counter
with open('log.txt') as f:
string = f.readlines()
c = Counter(string)
print c
它给了我所有重复行的结果,但我需要得到重复行(仅 2 次)
解决方案
您正在打印所有字符串,而不仅仅是重复的字符串,要仅打印重复两次的字符串,您可以打印计数为 2 的字符串。
from collections import Counter
with open('log.txt') as f:
string = f.readlines()
c = Counter(string)
for line, count in c.items():
if count==2:
print(line)
推荐阅读
- php - 如何在php中解析文件名并用某个名称重命名
- apache-nifi - NiFi ListAzureBlobStorage 与 HTTP 代理和身份验证
- php - 如何使用 PHP MongoDB 驱动程序解释 mongodb 查询计划
- python - 使用 blob.download_as_string 从 Cloud Storage 读取数据
- r - 分析包含 30 个观测值的数据集
- azure - 访问 Azure Cosmos DB 以在 power shell 中使用 Query 获取特定文档时未经授权的访问
- javascript - Cookie 存在时的条件不起作用(Js、Html)
- python - 如果组合框在 python gui 中可用,如何销毁它们?
- sql - SQL为每个属性添加总和行
- search - notepad++ - 搜索和替换单词并删除行