Remove Duplicates from Text File

前端 未结 7 1094
梦毁少年i
梦毁少年i 2020-12-17 05:34

I want to remove duplicate word from a text file.

i have some text file which contain such like following:

None_None

ConfigHandler_56663624
ConfigHa         


        
7条回答
  •  野趣味
    野趣味 (楼主)
    2020-12-17 05:50

    Here's about option that preserves order (unlike a set), but still has the same behaviour (note that the EOL character is deliberately stripped and blank lines are ignored)...

    from collections import OrderedDict
    
    with open('/home/jon/testdata.txt') as fin:
        lines = (line.rstrip() for line in fin)
        unique_lines = OrderedDict.fromkeys( (line for line in lines if line) )
    
    print unique_lines.keys()
    # ['None_None', 'ConfigHandler_56663624', 'ColumnConverter_56963312',PredicatesFactory_56963424', 'PredicateConverter_56963648', 'ConfigHandler_80134888']
    

    Then you just need to write the above to your output file.

提交回复
热议问题