 <p>To reorder the items, a basic way could be as follows:</p> <pre><code>split_line = line.split(" ") column_mapping = [9,6,3,7,3,2,1] reordered = [split_line[c] for c in column_mapping] joined = ",".join(reordered) outfile.write(joined) </code></pre> <p>This splits up the string, reorders it according to <code>column_mapping</code> and then combines it back into one string (comma separated)</p> <p>(in your code don't include <code>column_mapping</code> in the loop to avoid reinitialising it)</p>