techie2go wrote:
> i m now updating an sql file
> =================
> but the output is just
> 2003-01-04
> and not
>
> insert into mobilebill values ('2003-01-04', 'OUT', '91804103253', 34,
> 3.2);
>
> help me in this regard, what did i do wrong...
You can find out yourself if you throw in some print statements:
> for line in lines:
> #print line
> inp=line
> cre2=re.search('.*(\w{3}\s+\d+\,\s+\d+).*',line)
> (month,day,year)=cre2.group(1).split();
> cre3=re.compile(',')
> day=cre3.sub("",day)
> year='20'+year
> repstr=year+'-'+months[month]+'-'+day
print "X", repstr # OK, so far
> cre4=re.compile('.*(\w{3}\s+\d+\,\s+\d+).*')
print "X", cre4.search(line).group(0) # Oops, matches the whole line
# so it will be replaced by the
# reformatted date
> line=cre4.sub(repstr,line)
> print line
The minimal fix is to change cre4
cre4=re.compile('(\w{3}\s+\d+\,\s+\d+)')
And here is an alternative:
import re
import sys
lines = open('mb.sql')
months = {
"Jan": 1,
"Feb": 2,
"Mar": 3,
"Apr": 4,
# ...
}
r = re.compile(r"(\w{3})\s+(\d+)\,\s+(\d+)")
def fix_date(match):
month, day, year = match.groups()
return "20%02s-%02d-%02s" % (year, months[month], day)
for line in lines:
sys.stdout.write(r.sub(fix_date, line))
You might also consider performing the conversion in the database.
Peter
--
http://mail.python.org/mailman/listinfo/python-list