简体   繁体   中英

Python Datetime convert format into dd/mm/yyyy HH:MM:SS

please help me, I would need on two themes, the first, I read from an excel file of data, these will then be written in an XML, I fail because of the date, because I expect you to write the date in the format "dd/mm/yyyy hh:mm:ss", I tried different types of functions but with poor results

    s2 = ET1.SubElement(PmtInf, 'DATA')
    s2 = sheet.cell(row=2, column=3).value
    date_object = datetime.strptime(s2, '%d/%m/%y')

the second hel I would need, always in reading the excel file but being in a loop FOR i would like to be able to read from time to time the value of the top row in the sense 1 cycle -> row 1 col 1 2 cycle -> row 2 col 1 3 cycle -> row 3 col 1 ecc ecc what I felt was this:

for i in range(1,21):
        #first CdtTrfTxInf section with subsection
        CdtTrfTxInf = ET1.SubElement(PmtInf, 'CdtTrfTxInf')
        PmtId = ET1.SubElement(CdtTrfTxInf, 'PmtId')
        InstrId = ET1.SubElement(PmtId, 'InstrId')
        InstrId.text = sheet.cell(row=2, column=4).value
        InstrId = 'InstrId'
        EndToEndId = ET1.SubElement(PmtId, 'EndToEndId')
        EndToEndId.text = sheet.cell(row=2, column=3).value
        EndToEndId = 'EndToEndId'

But I realize that I never increase the value of the line, can you help me?

For dd/mm/yyyy hh:mm:ss you can use

date_object = datetime.strptime(s2, '%d/%m/%y %H:%M:%S')

For your second question, can you elaborate a bit more? I don't see you using variable 'i' anywhere inside the for loop. It is like you are looping over same data for 21 times.

Update: Looks like s2 is already a datetime object. This will provide you a date format in string output:

format_date = s2.strftime('%d/%m/%y %H:%M:%S')

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM