我有一个这样的txt文件:
he Cunctator 167 334555717
64.38.175.xxx 19 334555718
The Cunctator 172 334555721
AxelBoldt 21 334555723
AxelBoldt 21 334555723
24.112.58.xxx 175 334555726
24.112.58.xxx 175 334555726
24.112.58.xxx 175 334555726
Larry_Sanger 123 294277
Larry_Sanger 123 294277
现在需要做这样的处理: 就是第三列的这个id序列,要是有遇到了相同的id,则保留相同id的其中一行数据。比如结果应当如下:
1 he Cunctator 167 334555717
2 64.38.175.xxx 19 334555718
3 The Cunctator 172 334555721
4 AxelBoldt 21 334555723
5 24.112.58.xxx 175 334555726
6 Larry_Sanger 123 294277
请问该如何实现呢,另外,如上需要的结果显示,还需要给处理后的这个数据加上顺序编号。ORZ
因为输出是需要原来文件顺序的,所以不能使用set
巴扎黑2017-04-17 17:36:20
假設id相同的行都完全一致
with open('input', 'r') as f:
lines = f.readlines()
s = set()
counter = 0
with open('output', 'w') as f:
for line in lines:
if line in s: continue
counter += 1
f.write('%d %s\n' % (counter, line))
s.add(line)