Search code examples
pythonsortingstartswith

python read 2 files, ignore header (# beginning line), and sort data


How can I read two file at once, ignoring # lines, and sort them with their 2nd column values?

I was thinking of combination of .startswith(): and sorted(): command

    file1 = [line for line in open("file1.txt",'r').readlines() if not line.startswith("#")]
    file2 = [line for line in open("file2.txt",'r').readlines() if not line.startswith("#")]

    sorted_file1 = sorted(file1, key=lambda line: int(line.split()[1])) 
    sorted_file2 = sorted(file2, key=lambda line: int(line.split()[1]))

    do something fun using sorted files with for and if

My files are very simple. file 1 have values like

AAA 15125
BBB 69121
CCC 366161
.... 

and file 2 looks like

bkjnwg 11111
knksng 22155
bnkiop 13511
...

But I feel something strange about this code. How can I read, ignore #, and sort files more simply?

Thanks

Best,


Solution

  • Edited per comment.

    You can do:

    files_to_do = [...] #put paths in here
    for f in files_to_do:
        lines = [line for line in open(f,'r').readlines() if not line.startswith("#")]
        sorted_lines = sorted(lines, key=lambda line: int(line.split()[1]))
        #party