1
我正在使用python來解析XML文檔。我正在以XML格式列出時間。時間格式如下:將python中的當前時間與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>
感謝您的回覆。 現在我可以同時得到兩個數字,但計算機的時間是24小時的時間,而xml時間是12小時的時間,我認爲比較從不是真實的,所以打印和中斷從不運行。 – Matzunaga
XML有AM/PM,所以它工作正常。 –