Using python to find and convert a date string to integer in XML file? -


i have .xml file. anh want find date values in string replace each value new value after convert date integer. how can define function in python that? please me. thank you. example, have date in middle date , want convert , replace date string integer that: 12345678 , other data not change. here xml structure:

<?xml version="1.0" encoding="iso-8859-15"?> <output>     <response>         <error>0</error>     <response> <entries> <entry>     <line>0</line>     <id></id>     <first>2014-03-08</first>     <last>0</last>     <md5>12e00ed477ad6</md5>     <virustotal></virustotal>     <vt_score></vt_score>     <scanner></scanner>     <virusname></virusname>     <url>eva247.com/image/</url>     <recent></recent>     <response></response>     <ip></ip>     <as></as>     <review></review>     <domain>eva247.com</domain>     <country></country>     <source></source>     <email></email>     <inetnum></inetnum>     <netname>tam nhin moi</netname>     <descr></descr>     <ns1></ns1>     <ns2></ns2>     <ns3></ns3>     <ns4></ns4>     <ns5></ns5> </entry> <entry>     <line>1</line>     <id></id>     <first>2014-02-06</first>     <last>0</last>     <md5>de7db4cbe86d34373eb70</md5>     <virustotal></virustotal>     <vt_score></vt_score>     <scanner></scanner>     <virusname>n/a</virusname>     <url>files.downloadsmart.net</url>     <recent></recent>     <response></response>     <ip></ip>     <as>7643</as>     <review></review>     <domain>files.do</domain>     <country></country>     <source></source>     <email></email>     <inetnum></inetnum>     <netname>(vnpt)</netname>     <descr></descr>     <ns1></ns1>     <ns2></ns2>     <ns3></ns3>     <ns4></ns4>     <ns5></ns5> </entry> 

here code:

fp = open("2014-03-12_16.19.xml","r")  tmp = fp.read()  line in tmp:      fileopen = open("danh_sach_xml_new.xml","a")      match = re.search(r"(\d{4}-\d{2}-\d{2})", line)      if match:          result = match.group(1)          newline = result.replace(result,_timestamp_(result))          fileopen.write(newline)      else:          fileopen.write(line)      fileopen.close()  fp.close() 

and

def _timestamp_(date):      list_date = date.split("-")      pprint.pprint(list_date)      t = (int(list_date[0]), int(list_date[1]), int(list_date[2]),0,0,0)      time_stamp = time.mktime(t)      return time_stamp 

there many xml parsers available in python, elementtree has an example in documentation shows how modify xml file.

here how use that:

>>> import time >>> import datetime >>> import xml.etree.elementtree et >>> tree = et.parse('test.xml') >>> root = tree.getroot() >>> entry in root.find('entries').findall('entry'): ...     entry.find('first').text = str(time.mktime( ...                                datetime.datetime.strptime(entry.find('first').text, ...                                                           "%y-%m-%d").timetuple())) ... >>> tree.write('new.xml') 

Comments