I've got a CSV file that I'd like to convert into a different format. The CSV format is as follows:

```
A_to_B,B_to_C,C_to_D,...
0,2,1,...

```

I.e., each heading consists of two variables, e.g. A and B, and each row in the file contains a value that is either 0, 1, or 2. I'm trying to write a Python script that'll read this CSV file and turn it into a format like this:

```
A,B,0
B,C,2
C,D,1
...

```

In other words, it splits the first line which consists of headers into variables (e.g., A, B, C, D, etc.) and then matches the corresponding value for the new format. Does anyone have any idea how to do this? I have some of the basics down, but the actual algorithm I can't get my head around properly. Thanks for your help.

**UPDATE #1**

Here's some code I have down, but I don't get the correct output:

```
import csv,sys

reader = csv.reader(open(sys.argv[1], 'rt'), delimiter=',')
headers = reader.next()

data = []

for row in reader:
    line = ','.join(row)
    data.append(line)

for row in data:
    for cols, val in zip(headers, row):
        newRow = cols[0], cols[-1], val
        print newRow

```

The CSV file looks like this:

```
A,B,C
0,2,1
0,1,1

```

However, output of the code looks like this, so I need a way to iterate over the CSV file in the correct way:

```
('A', 'A', '0')
('B', 'B', ',')
('C', 'C', '2')
('A', 'A', '0')
('B', 'B', ',')
('C', 'C', '1')

```

**UPDATE #2**

If anyone stumbles onto this, here's the code I ended up with (no error handling or anything, but it works):

```
#!/usr/bin/python
# -*- coding: utf-8 -*-

import csv,os,sys

reader = csv.reader(open(sys.argv[1], 'rt'), delimiter=',')
headers = reader.next()
i = 1

for row in reader:
    os.system('rm id' + str(i) + '.csv')
    os.system('cat ./seeds >> id' + str(i) + '.csv')
    for srcdest,dist in zip(headers, row):
        sd = srcdest.split('_to_')
        src,dest = sd[0],sd[-1]
        if dist == '0':
            pass
        else:
            f = open('id' + str(i) + '.csv', 'a')
            f.write('{},{},{}\n'.format(src.lower().replace('_',''),dest.lower().replace('_',''),float(dist)))
    i=i+1

f.close()

```

*Thank you for your help everyone!*