0

I have a .XML file. Anh I want to find all date values in string then replace each value with new value after convert the date to integer. So how can I define a function in Python to do that? Please help me. Thank you. For Example, I have date in middle date and I want convert and replace date string to integer like that: 12345678 And all other data is not change. Here is my 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 is my code:

fp = open("2014-03-12_16.19.xml","r") 
tmp = fp.read() 
for 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
BenMorel
  • 34,448
  • 50
  • 182
  • 322
  • Have you tried anything yourself already? Python has a number of built-in XML modules that can read XML files (see [the documentation](http://docs.python.org/3.3/library/xml.html)). –  Mar 13 '14 at 09:22
  • This is the first time I working with python and XML, so I have many problem. I also try my function, but it not success. Here is my code: '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' – Phuong Tran Mar 13 '14 at 10:20
  • From your code, I only see you're converting the date. Nothing about reading XML data. So, what is your actual problem: reading the XML, or converting the date? –  Mar 13 '14 at 11:08
  • @Evert: I want to find and convert the date and then replace date with the converted value. Here is my code to find the date on XML file: 'fp = open("2014-03-12_16.19.xml","r") tmp = fp.read() for 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()' – Phuong Tran Mar 13 '14 at 15:53
  • I think problem is at convert function, but i dont know how to solve it? – Phuong Tran Mar 13 '14 at 15:58
  • You're asking two questions: how to read an element from the XML file, and how to convert a string to a date to an integer. Split that into two *separate* questions, and ask them separately. Or rather, search for the answer separately. For example: "python XML extract element value", and "python convert date string". –  Mar 13 '14 at 17:42
  • I searched but i did not know how to code a convert string to integer. So can you help me? – Phuong Tran Mar 14 '14 at 03:21
  • http://stackoverflow.com/questions/642154/how-to-convert-strings-into-integers-in-python ? –  Mar 14 '14 at 17:38

1 Answers1

0

There are many XML parsers available in Python, but ElementTree has an example in the documentation that shows you how to modify a XML file.

Here is how you would use that:

>>> import time
>>> import datetime
>>> import xml.etree.ElementTree as et
>>> tree = et.parse('test.xml')
>>> root = tree.getroot()
>>> for 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')
Burhan Khalid
  • 169,990
  • 18
  • 245
  • 284