Dear all,

I've been around and around with this and can't seem to conceptualize it properly.

I've got a javascript object in a text file that I'd like to treat as json so that I can import it into a python program via simplejson.loads(); however, it's not proper json because it has new Date() object declarations in it. So I thought to pre-process it by translating the dates into ISO format, but RE is making me cross-eyed.

example string:

s = """{"title" : "Hebertot, Normandie", "start" : new Date(1203,10,7), "description" : "Hardy's long name: Hebertot, Normandie. <br> <img src=\"document.png\" style=\"cursor: pointer\" onclick=\"SimileAjax.WindowManager.cancelPopups();show_next('tab3');pager('035'); return false\"/>pg.035: 1203-10-10 to 1203-11-18"},{"title" : "Newark-Upon-Trent, Nottinghamshire", "start" : new Date(1216,9,16), "end" : new Date(1216,9,18), "description" : "Hardy's long name: Newark-Upon-Trent, Nottinghamshire. <br> "}"""

I can locate the dates with:
jdate = re.compile('new Date\(\d{4},\d{1,2},\d{1,2}\)')

and:
jsdates = jdate.findall(s)

I can get a regex pattern that groups the necessary elements:
dateElems = re.compile('(\d{4}),(\d{1,2}),(\d{1,2})')

But I can't seem to put the bits together with re.sub() (or str.replace() ?) in the right sort of loop. How can I return the string with _all_ the dates changed in place thus:

"""{"title" : "Hebertot, Normandie", "start" : "1203-11-07"... etc.

instead of

"""{"title" : "Hebertot, Normandie", "start" : new Date(1203,10,7)... etc.

(js dates are 0 indexed, so Date(1203,10,7) is Nov. 7, 1203)

I'm sure this is a simple matter, but I'm just not looking at it right.

Jon
_______________________________________________
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor

Reply via email to