I am trying to use Python in order to manipulate a text file from Format A:
Key1
Key1value1
Key1value2
Key1value3
Key2
Key2value1
Key2value2
Key2value3
Key3...
Into Format B:
Key1 Key1value1
Key1 Key1value2
Key1 Key1value3
Key2 Key2value1
Key2 Key2value2
Key2 Key2value3
Key3 Key3value1...
Specifically, here is a brief look at the file itself (only one key shown, thousands more in the full file):
chr22:16287243: PASS
patientID1 G/G
patientID2 G/G
patient ID3 G/G
And the desired output here:
chr22:16287243: PASS patientID1 G/G
chr22:16287243: PASS patientID2 G/G
chr22:16287243: PASS patientID3 G/G
I've written the following code which can detect/display the keys, but I am having trouble writing the code to store the values associated with each key, and subsequently printing these key-value pairs. Can anyone please assist me with this task?
import sys
import re
records=[]
with open('filepath', 'r') as infile:
for line in infile:
variant = re.search("\Achr\d",line, re.I) # all variants start with "chr"
if variant:
records.append(line.replace("\n",""))
#parse lines until a new variant is encountered
for r in records:
print (r)