我有一个1,200行的文本文件.其中一些是重复的.
我怎么能找到文件中的重复行(但不要担心案例),然后在屏幕上打印出行的文字,这样我就可以找到它了?我不想删除它们或任何东西,只是找到它们可能是哪一行.
mgi*_*son 18
这套很简单:
with open('file') as f:
seen = set()
for line in f:
line_lower = line.lower()
if line_lower in seen:
print(line)
else:
seen.add(line_lower)
Run Code Online (Sandbox Code Playgroud)
因为只有1200行,所以你也可以使用collections.Counter()
:
>>> from collections import Counter
>>> with open('data1.txt') as f:
... c=Counter(c.strip().lower() for c in f if c.strip()) #for case-insensitive search
... for line in c:
... if c[line]>1:
... print line
...
Run Code Online (Sandbox Code Playgroud)
如果data1.txt
是这样的:
ABC
abc
aBc
CAB
caB
bca
BcA
acb
Run Code Online (Sandbox Code Playgroud)
输出是:
cab
abc
bca
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
22649 次 |
最近记录: |