 <pre><code>from itertools import izip with open("myfile.csv") as inf, open("new.csv","w") as outf: header = [s.split('_to_') for s in inf.next().split(',')] for row in inf: nums = (int(s) for s in row.split(',')) for (_from, _to), num in izip(header, nums): outf.write("{},{},{}\n".format(_from, _to, _num)) </code></pre>