Reputation: 903
I have a large file (example below) with data that I tried re-formatting using python (code below).
File:
Segment 30
XXXXXXXXXXXXXXXXXXXXXXXX
Sequuo: ju0
saeer on werarms23e: 610
//
Segment 30
XXXXXXXXXXXXXXXXXXXXXXXX
Sequuo: xu0
saeer on werarms23e: 400
//
I wanted to have a tab-delimited formatted outputs as below.
Expected output:
Sequuo saeer on werarms23e
ju0 610
xu0 s400
my attempt so far:
row=[]
with open("file.txt","r") as infile:
while True:
for line in infile:
if line.startswith("Sequuo:"):
line1=line.replace("\t","")
line1=line.split(":")
row.append(line1[1].strip())
#line1=line.replace("\n","\t")
if line.startswith("saeer on werarms23e:"):
line1=line.replace("\t","")
line1=line.split(":")
row.append(line1[1].strip())
print row
if line.startswith("//"):
break
if line=="":
break
Does anyone know how to get this code workin properly?
Thanks
Upvotes: 0
Views: 55
Reputation: 52071
If the expected o/p is
Sequuo saeer on werarms23e
ju0 610
xu0 400
Then try this code:
import sys
row=[]
with open("file.txt","r") as infile:
while True:
for line in infile:
if line.startswith("Sequuo:"):
line1=line.replace("\t","")
line1=line.split(":")
row.append(line1[1].strip())
#line1=line.replace("\n","\t")
if line.startswith("saeer on werarms23e:"):
line1=line.replace("\t","")
line1=line.split(":")
row.append(line1[1].strip())
if line.startswith("//"):
break
if line=="\n":
break
print ('Sequuo saeer on werarms23e')
count = 0
for i in row:
count +=1
sys.stdout.write (i + '\t')
if not count%2:
print
Upvotes: 1