Python - 显示文件中具有重复值列的所有行

时间:2015-06-25 10:09:04

标签: python

我想要搜索第5列(管道分隔)中的所有值和整个文件,并显示包含相同值的所有文件。 注意:第5列可能包含由昏迷分隔的多个值(00a0gte69876,2350gte69876)

输入文件: - 01001cee673753|5|011cb88f714093|001c7b203753|1cb34f767093|true|1|OldCPEPool|345311408905370 01001ffb673751|5|0100a044e69876|001c7b673745|00a0gte69876,2350gte69876|true|1|OldCPEPool|134340003134731 010016cww7f62f|5|017422cf6e565b|0016gtf7f62f|10gt828f2a13|true|3|OldCPEPool|141198213475041

我已尝试使用此代码在线但是,它给我带有open()

的错误
#!/usr/bin/env python
entries = []
duplicate_entries = []

with open('deviceBinding.org', 'r') as my_file:
    for line in my_file:
        columns = line.strip().split(',')
        if columns[2] not in entries:
            entries.append(columns[2])
        else:
           duplicate_entries.append(columns[2]) 

if len(duplicate_entries) > 0:
    with open('out.txt', 'w') as out_file:
        with open('deviceBinding.org', 'r') as my_file:
            for line in my_file:
                columns = line.strip().split(',')
                if columns[2] in duplicate_entries:
                    print line.strip()
                    out_file.write(line)
else:
    print "No repetitions"

错误: File "a.py", line 5 with open('deviceBinding.org', 'r') as my_file: 请帮帮我。

0 个答案:

没有答案