如何解决如何解决Python SyntaxError:in的问题,如果使用可变确定的地图图标将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','32.621384','-83.620274','606 McArthur Blvd,'eCommunity Warner Robins')
('36-E6-AD-E5-AC-77','Off','32.623730','-83.620145',GA 3109','eCommunity Warner Robins')
('68-F7-28-ED-12-FA','Ready','32.624298','-83.627544','112 Anne Dr,'eCommunity Warner Robins')
('00-FF-A8-F3-3D-28','Active','32.620851','-83.630560','410 Bernard Dr,'eCommunity Warner Robins')
(None,None,'32.623577','-83.628145','101 Gordon St,'eCommunity Warner Robins')
解决方法
答案出在错误中:
文件“”,如果cpeStatus ='Active',则在第26行:^ 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
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。