Hi all
How can I create a csv file with header. I have a text file with several number of blocks staring from "//" and ending a block with "//". I have attached a sample file.
I want to use first column of this text as a header of csv and append associated values in it. If any of the header missing in a block create new and append.
for example from text below, I want a csv with AC, ID, FA, OS, SF, BS, GE as an header and their values under that header. Could anyone help me with this. I have tried doing this using code at the end. But not getting exactly what I want.
//
AC T876837378768
XX
ID T876837378768
XX
DT 16.09.1996 (created); ewi.
CO Copyright (C), Biobase GmbH.
XX
FA MNG345
XX
OS human, Homo sapiens
OC eukaryota; animalia; metazoa; chordata; vertebrata; tetrapoda; mammalia; eutheria; primates
XX
SF similar to MNG;
XX
FF induced by interferon-alpha (15-30'), inhibited by 2-AP;
XX
BS R02116; AAF$CONS; Quality: 6.
BS R03064; HS$GBP_02; Quality: 6; GBP, G000264; human, Homo sapiens.
XX
DR TRANSPATH: MO000026034.
XX
RN [1]; RE0000446.
RX PUBMED: 1901265.
RA Decker T., Lew D. J., Mirkowitch J., Darnell J. E.
RT Cytoplasmic activation of GAF, an IFN-gamma-regulated DNA-binding factor
RL EMBO J. 10:927-932 (1991).
RN [2]; RE0001471.
RX PUBMED: 1833631.
RA Decker T., Lew D. J., Darnell J. E.
RT Two distinct alpha-interferon-dependent signal transduction pathways may contribute to activation of transcription of the guanylate-binding protein gene
RL Mol. Cell. Biol. 11:5147-5153 (1991).
XX
//
tfid,fa,os,ge,osm,ins,inm = "","","","","","",""
for line in f1 :
r1 = line.split()
if line.startswith("ID"):
tfid = r1[1]
#print a
if line.startswith("FA"):
fa = r1[1]
#print b
if line.startswith("OS")and line.endswith("sapiens\n"):
os = " ".join(r1[1:])
#print os
if line.startswith("GE"):
ge = " ".join(r1[1:3])
#print ge
if line.startswith("OS")and line.endswith("Mammalia\n"):
osm = r1[1]
#print c
if line.startswith("IN") and line.endswith("sapiens.\n"):
ins ="\t".join(r1[1:3])
#print g
if line.startswith("IN") and line.endswith("Mammalia.\n"):
inm = "\t".join(r1[1:])
if line.startswith("//"):
tftable = os+"\t"+tfid+"\t"+fa+"\t"+"\t"+ge+"\t"+osm+"\t"+ins+"\t"+inm+"\n"
#tfid,fa,os,ge,osm,ins,inm = "","","","","","",""