Hi i have an input file in the below format.
.....
......
<TABLE COLS="3">
<ROW>
<R>data</R>
<R>data</R>
</ROW>
<ROW>
<R>data</R>
<R>data</R>
<R>data</R>
</ROW>
</TABLE>
<TABLE COLS="4">
<ROW>
<R>data</R>
<R>data</R>
<R>data</R>
<R>data</R>
<R>data</R>
</ROW>
<ROW>
<R>data</R>
<R>data</R>
</ROW>
</TABLE>
.......
.....
.
...
The output file should be as :
....
....
.
..
<table ct="3">
<ent="1">
<ent="2">
<ent="3">
<row>
<rvn ="1">data</rvn>
<rvn ="2">data</rvn>
</row>
<row>
<rvn ="1">data</rvn>
<rvn ="2">data</rvn>
<rvn ="3">data</rvn>
</row>
</table>
<table ct="4">
<ent="1">
<ent="2">
<ent="3">
<ent="4">
<row>
<rvn ="1">data</rvn>
<rvn ="2">data</rvn>
<rvn ="3">data</rvn>
<rvn ="4">data</rvn>
<rvn ="5">data</rvn>
</row>
<row>
<rvn ="1">data</rvn>
<rvn ="2">data</rvn>
</row>
</table>
...
...
...
i have writen the below code: when i run this code the table col value is being replaced by the last table col value. and also i am facing problem in incrementing the <rvn>
value. can any one of you please help me to solve the problem.
import re
def tblcnv( st, val ):
Tcolspec = ''
Endval = int(val) + 1
for i in range(1, Endval):
l = str(i)
Tcolspec += "<colspec col='" + l + "' colwidth=''/>\n"
Theader = re.sub(r"(?i)<table.*?>","<table ct='" + val +"'>\n" + Tcolspec + "\n", st)
return Theader
in_data = open("in.txt", "r")
out_data = open("out.txt", "w")
Rdata = in_data.read()
Rval = Rdata.replace("\n", " ")
Rval = re.sub("(?i)(<TABLE.*cols=\"(\d+).*?</TABLE>)", lambda m: tblcnv(m.group(1), m.group(2)), Rval)
out_data.write(Rval)