Question: First time posting here so please be patient!

I have a file that looks like that:

```
POS {ALLELE:COUNT}  
1   G:27    A:11
2   C:40    T:0
3   C:40    A:0
4   T:40    G:0
5   G:0 C:40
6   C:40    T:0
7   G:24    A:14
8   G:40    A:0
9   A:40    G:0
...

```

I want to combine the information from the second and third column for each line in the following format: "number[A],number[C],number[G],number[T]" so that the example above would look like that:

```
POS {ALLELE:COUNT}
1   11,0,27,0
2   0,40,0,0
3   0,40,0,0
4   0,0,0,40
5   0,40,0,0
6   0,40,0,0
7   14,0,24,0
8   0,0,40,0
9   40,0,0,0
...

```

Any idea on how I could do that would be much appreciated!

Answer: 
Here's a method that works:

```
lines = open('test.txt','r').read().splitlines()

place = {'A':0,'C':1,'G':2,'T':3}
counts = [[0 for _ in range(4)] for _ in range(len(lines[1:]))]
for i,row in enumerate(lines[1:]):
    for ct in row.split()[1:]:
        a,b = ct.split(':')
        counts[i][place[a]] = int(b)

out_str = '\n'.join([lines[0]] + ['{:<4}{},{},{},{}'.format(i+1,*ct) 
    for i,ct in enumerate(counts)])

with open('output.txt','w') as f:
    f.write(out_str)

```

The resulting file reads

```
POS {ALLELE:COUNT}  
1   11,0,27,0
2   0,40,0,0
3   0,40,0,0
4   0,0,0,40
5   0,40,0,0
6   0,40,0,0
7   14,0,24,0
8   0,0,40,0
9   40,0,0,0

```
sourse.txt

```
POS {ALLELE:COUNT}
1   G:27    A:11
2   C:40    T:0
3   C:40    A:0
4   T:40    G:0
5   G:0 C:40
6   C:40    T:0
7   G:24    A:14
8   G:40    A:0
9   A:40    G:0

```

```py
import re

template = ('A', 'C', 'G', 'T')

def proc_line(line: str):

    index, *elements = re.findall(r'\S+', line)

    data = dict([*map(lambda x: x.split(':'), elements)])

    return f'{index}\t' + ','.join([data.get(item, '0') for item in template]) + '\n'

with open('source.txt', encoding='utf-8') as file:
    header, *lines = file.readlines()

with open('output.txt', 'w', encoding='utf-8') as new_file:
    new_file.writelines(
        [header] + list(map(proc_line, lines))
    )

```

output.txt

```
POS {ALLELE:COUNT}  
1   11,0,27,0
2   0,40,0,0
3   0,40,0,0
4   0,0,0,40
5   0,40,0,0
6   0,40,0,0
7   14,0,24,0
8   0,0,40,0
9   40,0,0,0

```