将python中的当前时间与从xml解析的时间进行比较

时间:2017-02-07 03:19:48

标签: python xml parsing time

我正在使用python来解析XML文档。我得到了XML中列出的时间。时间采用以下格式:

<SCHEDULED_START_TIME>5:16:26 PM</SCHEDULED_START_TIME>

我需要将它与计算机中的当前时间进行比较。

根据比较,我需要将同一节点中的数字分配给一个变量,然后该变量将被传输到另一个程序。该号码采用以下格式:

<SCHEDULED_HOUSE_NAM>149</SCHEDULED_HOUSE_NAM>

这是我到目前为止所做的,但在比较之后,我要么得到第一个数字,要么得到0(零)。

到目前为止

代码:

import xml.dom.minidom as minidom
from datetime import datetime

sponsorTimeArray = []
houseNumberArray = []
sponsorTimeIndex = 0
currentHouseNumber = 0


def readFeed():
    #myTime = datetime.now().strftime('%I:%M:%S %p').lstrip('0')
    #print myTime

    sponsorsXML = minidom.parse("/Users/jobregon/Desktop/test_output.xml")

    sponsorTimes = sponsorsXML.getElementsByTagName("ITEM")

    for sprTime in sponsorTimes:
        currentTime = sprTime.getElementsByTagName("SCHEDULED_START_TIME")[0].firstChild.data
        if currentTime not in sponsorTimeArray:
            sponsorTimeArray.append(currentTime)
#   for each in sponsorTimeArray:
#       print each

    houseNumbers = sponsorsXML.getElementsByTagName("ITEM")

    for hsNumber in houseNumbers:
        currentHousNumber = hsNumber.getElementsByTagName("SCHEDULED_HOUSE_NAM")[0].firstChild.data
        if currentHousNumber not in houseNumberArray:
            houseNumberArray.append(currentHousNumber)
#   for each in houseNumberArray:
#       print each


    for i in sponsorTimeArray:
        global sponsorTimeIndex
        global currentHouseNumber
        #print myTime
        #currentHouseNumber = houseNumberArray[sponsorTimeIndex]
        if datetime.now().lstrip('0') <= datetime.strptime(i,'%I:%M:%S %p'):
            print 'running if'
            currentHouseNumber = houseNumberArray[5]
            break
        sponsorTimeIndex += 1


readFeed()

XML_DATA:

<ROOT>
  <DATA>
    <ITEM>
      <SCHEDULED_START_TIME>4:30:00 AM</SCHEDULED_START_TIME>
      <SCHEDULED_HOUSE_NAM>166</SCHEDULED_HOUSE_NAM>
    </ITEM>
    <ITEM>
      <SCHEDULED_START_TIME>4:37:29 AM</SCHEDULED_START_TIME>
      <SCHEDULED_HOUSE_NAM>166</SCHEDULED_HOUSE_NAM>
    </ITEM>
    <ITEM>
      <SCHEDULED_START_TIME>4:45:03 AM</SCHEDULED_START_TIME>
      <SCHEDULED_HOUSE_NAM>166</SCHEDULED_HOUSE_NAM>
    </ITEM>
    <ITEM>
      <SCHEDULED_START_TIME>4:52:37 AM</SCHEDULED_START_TIME>
      <SCHEDULED_HOUSE_NAM>166</SCHEDULED_HOUSE_NAM>
    </ITEM>
    <ITEM>
      <SCHEDULED_START_TIME>5:00:00 AM</SCHEDULED_START_TIME>
      <SCHEDULED_HOUSE_NAM>103</SCHEDULED_HOUSE_NAM>
    </ITEM>
    <ITEM>
      <SCHEDULED_START_TIME>5:10:06 AM</SCHEDULED_START_TIME>
      <SCHEDULED_HOUSE_NAM>103</SCHEDULED_HOUSE_NAM>
    </ITEM>
    <ITEM>
      <SCHEDULED_START_TIME>5:20:17 AM</SCHEDULED_START_TIME>
      <SCHEDULED_HOUSE_NAM>103</SCHEDULED_HOUSE_NAM>
    </ITEM>
    <ITEM>
      <SCHEDULED_START_TIME>5:30:18 AM</SCHEDULED_START_TIME>
      <SCHEDULED_HOUSE_NAM>136</SCHEDULED_HOUSE_NAM>
    </ITEM>
    <ITEM>
      <SCHEDULED_START_TIME>5:40:19 AM</SCHEDULED_START_TIME>
      <SCHEDULED_HOUSE_NAM>136</SCHEDULED_HOUSE_NAM>
    </ITEM>
    <ITEM>
      <SCHEDULED_START_TIME>5:50:25 AM</SCHEDULED_START_TIME>
      <SCHEDULED_HOUSE_NAM>136</SCHEDULED_HOUSE_NAM>
    </ITEM>
    <ITEM>
      <SCHEDULED_START_TIME>5:59:45 AM</SCHEDULED_START_TIME>
      <SCHEDULED_HOUSE_NAM>146</SCHEDULED_HOUSE_NAM>
    </ITEM>
    <ITEM>
      <SCHEDULED_START_TIME>6:10:09 AM</SCHEDULED_START_TIME>
      <SCHEDULED_HOUSE_NAM>165</SCHEDULED_HOUSE_NAM>
    </ITEM>
    <ITEM>
      <SCHEDULED_START_TIME>6:20:33 AM</SCHEDULED_START_TIME>
      <SCHEDULED_HOUSE_NAM>146</SCHEDULED_HOUSE_NAM>
    </ITEM>
    <ITEM>
      <SCHEDULED_START_TIME>6:31:07 AM</SCHEDULED_START_TIME>
      <SCHEDULED_HOUSE_NAM>146</SCHEDULED_HOUSE_NAM>
    </ITEM>
    <ITEM>
      <SCHEDULED_START_TIME>6:41:36 AM</SCHEDULED_START_TIME>
      <SCHEDULED_HOUSE_NAM>109</SCHEDULED_HOUSE_NAM>
    </ITEM>
    <ITEM>
      <SCHEDULED_START_TIME>6:52:10 AM</SCHEDULED_START_TIME>
      <SCHEDULED_HOUSE_NAM>146</SCHEDULED_HOUSE_NAM>
    </ITEM>
    <ITEM>
      <SCHEDULED_START_TIME>7:08:20 AM</SCHEDULED_START_TIME>
      <SCHEDULED_HOUSE_NAM>170</SCHEDULED_HOUSE_NAM>
    </ITEM>
    <ITEM>
      <SCHEDULED_START_TIME>7:26:00 AM</SCHEDULED_START_TIME>
      <SCHEDULED_HOUSE_NAM>163</SCHEDULED_HOUSE_NAM>
    </ITEM>
    <ITEM>
      <SCHEDULED_START_TIME>7:42:44 AM</SCHEDULED_START_TIME>
      <SCHEDULED_HOUSE_NAM>124</SCHEDULED_HOUSE_NAM>
    </ITEM>
    <ITEM>
      <SCHEDULED_START_TIME>7:56:00 AM</SCHEDULED_START_TIME>
      <SCHEDULED_HOUSE_NAM>154</SCHEDULED_HOUSE_NAM>
    </ITEM>
    <ITEM>
      <SCHEDULED_START_TIME>8:05:22 AM</SCHEDULED_START_TIME>
      <SCHEDULED_HOUSE_NAM>181</SCHEDULED_HOUSE_NAM>
    </ITEM>
    <ITEM>
      <SCHEDULED_START_TIME>8:25:00 AM</SCHEDULED_START_TIME>
      <SCHEDULED_HOUSE_NAM>104</SCHEDULED_HOUSE_NAM>
    </ITEM>
    <ITEM>
      <SCHEDULED_START_TIME>8:34:07 AM</SCHEDULED_START_TIME>
      <SCHEDULED_HOUSE_NAM>170</SCHEDULED_HOUSE_NAM>
    </ITEM>
    <ITEM>
      <SCHEDULED_START_TIME>8:55:05 AM</SCHEDULED_START_TIME>
      <SCHEDULED_HOUSE_NAM>105</SCHEDULED_HOUSE_NAM>
    </ITEM>
    <ITEM>
      <SCHEDULED_START_TIME>11:00:00 AM</SCHEDULED_START_TIME>
      <SCHEDULED_HOUSE_NAM>166</SCHEDULED_HOUSE_NAM>
    </ITEM>
    <ITEM>
      <SCHEDULED_START_TIME>11:09:01 AM</SCHEDULED_START_TIME>
      <SCHEDULED_HOUSE_NAM>166</SCHEDULED_HOUSE_NAM>
    </ITEM>
    <ITEM>
      <SCHEDULED_START_TIME>11:17:52 AM</SCHEDULED_START_TIME>
      <SCHEDULED_HOUSE_NAM>188</SCHEDULED_HOUSE_NAM>
    </ITEM>
    <ITEM>
      <SCHEDULED_START_TIME>11:26:48 AM</SCHEDULED_START_TIME>
      <SCHEDULED_HOUSE_NAM>106</SCHEDULED_HOUSE_NAM>
    </ITEM>
    <ITEM>
      <SCHEDULED_START_TIME>11:35:44 AM</SCHEDULED_START_TIME>
      <SCHEDULED_HOUSE_NAM>104</SCHEDULED_HOUSE_NAM>
    </ITEM>
    <ITEM>
      <SCHEDULED_START_TIME>11:44:35 AM</SCHEDULED_START_TIME>
      <SCHEDULED_HOUSE_NAM>106</SCHEDULED_HOUSE_NAM>
    </ITEM>
    <ITEM>
      <SCHEDULED_START_TIME>11:53:46 AM</SCHEDULED_START_TIME>
      <SCHEDULED_HOUSE_NAM>106</SCHEDULED_HOUSE_NAM>
    </ITEM>
    <ITEM>
      <SCHEDULED_START_TIME>4:59:30 PM</SCHEDULED_START_TIME>
      <SCHEDULED_HOUSE_NAM>149</SCHEDULED_HOUSE_NAM>
    </ITEM>
    <ITEM>
      <SCHEDULED_START_TIME>5:07:58 PM</SCHEDULED_START_TIME>
      <SCHEDULED_HOUSE_NAM>180</SCHEDULED_HOUSE_NAM>
    </ITEM>
    <ITEM>
      <SCHEDULED_START_TIME>5:16:26 PM</SCHEDULED_START_TIME>
      <SCHEDULED_HOUSE_NAM>149</SCHEDULED_HOUSE_NAM>
    </ITEM>
    <ITEM>
      <SCHEDULED_START_TIME>5:25:14 PM</SCHEDULED_START_TIME>
      <SCHEDULED_HOUSE_NAM>173</SCHEDULED_HOUSE_NAM>
    </ITEM>
    <ITEM>
      <SCHEDULED_START_TIME>5:34:02 PM</SCHEDULED_START_TIME>
      <SCHEDULED_HOUSE_NAM>114</SCHEDULED_HOUSE_NAM>
    </ITEM>
    <ITEM>
      <SCHEDULED_START_TIME>5:42:50 PM</SCHEDULED_START_TIME>
      <SCHEDULED_HOUSE_NAM>111</SCHEDULED_HOUSE_NAM>
    </ITEM>
    <ITEM>
      <SCHEDULED_START_TIME>5:51:38 PM</SCHEDULED_START_TIME>
      <SCHEDULED_HOUSE_NAM>173</SCHEDULED_HOUSE_NAM>
    </ITEM>
    <ITEM>currentHouseNumber = houseNumberArray[sponsorTimeIndex]
      <SCHEDULED_START_TIME>6:28:56 PM</SCHEDULED_START_TIME>
      <SCHEDULED_HOUSE_NAM>100</SCHEDULED_HOUSE_NAM>
    </ITEM>
    <ITEM>
      <SCHEDULED_START_TIME>6:36:26 PM</SCHEDULED_START_TIME>
      <SCHEDULED_HOUSE_NAM>117</SCHEDULED_HOUSE_NAM>
    </ITEM>
    <ITEM>
      <SCHEDULED_START_TIME>6:44:16 PM</SCHEDULED_START_TIME>
      <SCHEDULED_HOUSE_NAM>128</SCHEDULED_HOUSE_NAM>
    </ITEM>
    <ITEM>
      <SCHEDULED_START_TIME>6:52:11 PM</SCHEDULED_START_TIME>
      <SCHEDULED_HOUSE_NAM>100</SCHEDULED_HOUSE_NAM>
    </ITEM>
    <ITEM>
      <SCHEDULED_START_TIME>11:00:30 PM</SCHEDULED_START_TIME>
      <SCHEDULED_HOUSE_NAM>122</SCHEDULED_HOUSE_NAM>
    </ITEM>
    <ITEM>
      <SCHEDULED_START_TIME>11:07:13 PM</SCHEDULED_START_TIME>
      <SCHEDULED_HOUSE_NAM>120</SCHEDULED_HOUSE_NAM>
    </ITEM>
    <ITEM>
      <SCHEDULED_START_TIME>11:14:06 PM</SCHEDULED_START_TIME>
      <SCHEDULED_HOUSE_NAM>122</SCHEDULED_HOUSE_NAM>
    </ITEM>
    <ITEM>
      <SCHEDULED_START_TIME>11:20:49 PM</SCHEDULED_START_TIME>
      <SCHEDULED_HOUSE_NAM>142</SCHEDULED_HOUSE_NAM>
    </ITEM>
    <ITEM>
      <SCHEDULED_START_TIME>11:28:12 PM</SCHEDULED_START_TIME>
      <SCHEDULED_HOUSE_NAM>122</SCHEDULED_HOUSE_NAM>
    </ITEM>
  </DATA>
</ROOT>

1 个答案:

答案 0 :(得分:0)

要进行时间比较,您需要执行以下操作:

sponsor_time = datetime.strptime(i, '%I:%M:%S %p').time()
now_time = datetime.now().time()
if now_time <= sponsor_time:
    print('running if @ %s' % sponsor_time)
    break

此处的关键元素是将datetime.datetime转换为datetime.time以允许仅使用时间的比较,并忽略日期。