如何解决Python语法错误:在if调用中,同时将dB数据发布到带有可变映射图标的kml



我想使用Python每5分钟从MS数据库中查询一次数据,并将其发布到kml中,以便多人通过网络链接查看。我在代码的末尾尝试使用if-elif来确定每行结果显示哪个占位符图标。这是我的示例代码:

import simplekml
kml = simplekml.Kml()
pnt = kml.newpoint(name="cpeMAC", coords=[(cpeLON,cpeLAT)])
pnt.description = "FullAddress","Network"
pnt.snippet.content = "cpeMAC","cpeStatus"
pnt.snippet.maxlines = 1
pnt.lookat = simplekml.LookAt(gxaltitudemode=simplekml.GxAltitudeMode.relativetoseafloor,
latitude= cpeLAT, longitude= cpeLON, 
range=3000, heading=56, tilt=0) 
# The above range, heading and tilt should be hard coded. 
# The icon’s for each placemark should change based on the values in cpeStatus. There are 5 output types that can be passed from the dB: None, Off, Repair, Active, Alarm. 
# Below is the code that I need the most help with 
if  cpeStatus = 'Active':
pnt.style.iconstyle.icon.href = "http://maps.google.com/mapfiles/kml/shapes/ranger_station.png"

elif cpeStatus = 'Off':
pnt.style.iconstyle.icon.href = "http://maps.google.com/mapfiles/kml/shapes/forbidden.png"
elif cpeStatus = 'Ready':
pnt.style.iconstyle.icon.href = "http://maps.google.com/mapfiles/kml/shapes/mechanic.png"
elif cpeStatus = 'Alarm':
pnt.style.iconstyle.icon.href = "http://maps.google.com/mapfiles/kml/shapes/caution.png"
return stat
# The file should then be saved to kml stored on the local server. 
kml.save("PremiseCPEStatus3.kml")

我得到以下一致错误:

File "<ipython-input-24-49c8d14fb0c8>", line 26
if  cpeStatus = 'Active':
^
SyntaxError: invalid syntax

作为参考,数据库查询正在成功提取以下信息:我用空格分隔各行,以便更容易阅读

('124-22-A9-12-44-88', 'Alarm', '32.622399444', '-83.60337837', '365 Johns Rd, Warner Robins, GA 31093', 'eCommunity Warner Robins')  
('34-E6-AD-E5-AC-77', 'Alarm', '32.621384', '-83.620274', '606 McArthur Blvd, Warner Robins, GA 31093', 'eCommunity Warner Robins')  
('36-E6-AD-E5-AC-77', 'Off', '32.623730', '-83.620145', '365 Johns Rd, Warner Robins, GA 3109', 'eCommunity Warner Robins')  
('68-F7-28-ED-12-FA', 'Ready', '32.624298', '-83.627544', '112 Anne Dr, Warner Robins, GA 3109', 'eCommunity Warner Robins')  
('00-FF-A8-F3-3D-28', 'Active', '32.620851', '-83.630560', '410 Bernard Dr, Warner Robins, GA 3109', 'eCommunity Warner Robins')  
(None, None, '32.623577', '-83.628145', '101 Gordon St, Warner Robins, GA 3109', 'eCommunity Warner Robins')

答案在错误中:

文件";,第26行,如果cpeStatus="活动":^SyntaxError:无效语法

应为:

if cpeStatus == 'Active'

否则,您将在if语句期间进行赋值,而不是检查其值,即无效语法。

此外,我会考虑一些可读性更强的东西。我会把它构造成这样(你不需要缩进,因为它都在测试同一个var(——

icon_status = {
'Active': 'http://maps.google.com/mapfiles/kml/shapes/ranger_station.png',
'Off': 'http://maps.google.com/mapfiles/kml/shapes/forbidden.png'
'Ready': 'http://maps.google.com/mapfiles/kml/shapes/mechanic.png',
'Alarm' 'http://maps.google.com/mapfiles/kml/shapes/caution.png'
}
if  cpeStatus = 'Active':
pnt.style.iconstyle.icon.href = icon_status['Active']
elif cpeStatus == 'Off':
pnt.style.iconstyle.icon.href = icon_status['Off']
elif cpeStatus == 'Ready':
pnt.style.iconstyle.icon.href = icon_status['Ready']
elif cpeStatus == 'Alarm':
pnt.style.iconstyle.icon.href = icon_status['Alarm']
return stat

最新更新