Replacing a column with another column I wrote this below script which basically replaces the one of the column in the df with another column with incremental IDs. The script works perfectly fine but I was wondering if there is a better/efficient way to write this one. 

import sys

fh_in = open(sys.argv[1], 'r')
fh_out = open(sys.argv[2], 'w')

count = 1
dict = {}
final = list()

for line in fh_in:
    line = line.strip()
    line = line.split()
    if count &lt; 10:
        dict[line[3]] = "Bra100000" + str(count)
        count = count + 1
    else:
        dict[line[3]] = "Bra10000" + str(count)
        count = count + 1
    test = "{:10s}" .format(dict[line[3]])
    fh_out.write(line[0])
    fh_out.write("\t")
    fh_out.write(line[1])
    fh_out.write("\t")
    fh_out.write(line[2])
    fh_out.write("\t")
    fh_out.write(line[1])
    fh_out.write("\t")
    fh_out.write(test)
    fh_out.write("\t")
    fh_out.write(line[4])
    fh_out.write("\t")
    fh_out.write(line[5])
    fh_out.write("\t")
    fh_out.write(line[6])
    fh_out.write("\t")
    fh_out.write(line[7])
    fh_out.write("\t")
    fh_out.write(line[8])
    fh_out.write("\t")
    fh_out.write(line[9])
    fh_out.write("\t")
    fh_out.write(line[10])
    fh_out.write("\t")
    fh_out.write(line[11])
    fh_out.write("\n")


Input:



A01     158188  158533  Contig545|m.1503        228     +       158188  158533  255,0,0 1       345     0
A01     272139  272465  comp285432_c0_seq1|m.9436       230     -       272139  272465  255,0,0 1       326     0
A01     339617  340806  TCONS_00003584|m.11226  157     -       339617  340806  255,0,0 5       95,127,68,50,432        0,190,467,619,757
A01     888838  889347  Contig1477|m.3679       92      -       888838  889347  255,0,0 1       509     0
A01     1165488 1165761 comp3043338_c0_seq1|m.9546      228     -       1165488 1165761 255,0,0 1       273     0
A01     1167009 1167386 Contig1598|m.4011       238     -       1167009 1167386 255,0,0 1       377     0
A01     1234347 1234827 comp260850_c0_seq1|m.10201      85      +       1234347 1234827 255,0,0 1       480     0


Output:

A01     158188  158533  158188  Bra1000001      228     +       158188  158533  255,0,0 1       345     0
A01     272139  272465  272139  Bra1000002      230     -       272139  272465  255,0,0 1       326     0
A01     339617  340806  339617  Bra1000003      157     -       339617  340806  255,0,0 5       95,127,68,50,432        0,190,467,619,757
A01     888838  889347  888838  Bra1000004      92      -       888838  889347  255,0,0 1       509     0
A01     1165488 1165761 1165488 Bra1000005      228     -       1165488 1165761 255,0,0 1       273     0
A01     1167009 1167386 1167009 Bra1000006      238     -       1167009 1167386 255,0,0 1       377     0
A01     1234347 1234827 1234347 Bra1000007      85      +       1234347 1234827 255,0,0 1       480     0
It would be a whole lot better this way:

import sys


def main(filename_in, filename_out):
    with open(filename_in) as fh_in:
        with open(filename_out, 'w') as fh_out:
            count = 1
            for line in fh_in:
                parts = line.strip().split()
                if count &lt; 10:
                    bra = "Bra100000" + str(count)
                else:
                    bra = "Bra10000" + str(count)
                count += 1
                test = "{:10s}" .format(bra)
                fh_out.write("\t".join(parts[0:3]))
                fh_out.write("\t")
                fh_out.write(parts[1])
                fh_out.write("\t")
                fh_out.write(test)
                fh_out.write("\t")
                fh_out.write("\t".join(parts[4:12]))
                fh_out.write("\n")

if __name__ == '__main__':
    main(sys.argv[1], sys.argv[2])


Improvements:


Use with open(...) as ...:. This way the file handles will be correctly closed when exiting the block
Drop the unused variable final
Also drop the pointless variable dict
Since both branches of the if-else increment count, you can move it outside
Instead of count = count + 1, it's simpler as count += 1
Instead of the repetitive write, simplify with join
Do not just dump the code in the global namespace: put it inside a function. This way the command line arguments will have meaningful names
Instead of stripping and splitting line in 2 steps, do it in one: line.strip().split()
It's not a good practice to overwrite loop variables and function parameters. line is a good name for a line of input, splitting that becomes something else and "line" is no longer an appropriate name, so I called it "parts" instead