re.findall produces array like list in Python

Viewed 30

I am working on a python script to extract couple of features from a text file.

The input file has following structure:

ENTRY       M00001            Pathway   Module
NAME        Glycolysis (Embden-Meyerhof pathway), glucose => pyruvate
CLASS       Pathway modules; Carbohydrate metabolism; Central carbohydrate metabolism
PATHWAY     map00010  Glycolysis / Gluconeogenesis
            map01200  Carbon metabolism
            map01100  Metabolic pathways

///

I am extracting value from 'ENTRY' field and 'PATHWAY' field. However, I am getting below as an output when I write the content to PostgreSQL 11.0 table.The column type is 'character varying'

id          map_id
{M00001}    {map00010,map01200,map01100}
{M00002}    {map00010,map01200,map01230,map01100}
{M00003}    {map00010,map00020,map01100}
{M00004}    {map00030,map01200,map01100,map01120}
{M00005}    {map00030,map00230,map01200,map01230,map01100}

Below code generate the above output:

cursor = conn.cursor()
dict = {}
with open ('file') as f:                                                                                   
    for line in f:
        if(re.search("^[A-Z]", line) ):
            key, value = re.split("\s+", line, 1)
            dict[key] = value
        elif(re.search("^\s+", line)):
            dict[key] = dict[key] + line
        elif(re.search("^///", line)):
            e = dict['ENTRY']
            string = ''.join(e)
            id = re.findall(r"(^[A-Za-z+]\d+)", string)                        
            map_id = re.findall(r"(map\d+)\s+.*",dict['PATHWAY'])
             
            cursor.execute("INSERT INTO tbl (id, map_id) VALUES (%s, %s)",(id, map_id))


conn.commit()
conn.close()
cursor.close()

The expected output is:

id          map_id
M00001  map00010,map01200,map01100
M00002  map00010,map01200,map01230,map01100
M00003  map00010,map00020,map01100
M00004  map00030,map01200,map01100,map01120
M00005  map00030,map00230,map01200,map01230,map01100

Any help is highly appreciated

0 Answers
Related