Python 在django模型上存储xml数据
我正在尝试将msn气象服务的XML文件数据存储在我的模型上,您可以看到下面的代码Python 在django模型上存储xml数据,python,xml,django,Python,Xml,Django,我正在尝试将msn气象服务的XML文件数据存储在我的模型上,您可以看到下面的代码 class Command(NoArgsCommand): help = "This will update the weather reports by fetching xml from msn and storing data" def handle_noargs(self, **options): tree = ElementTree() path = ur
class Command(NoArgsCommand):
help = "This will update the weather reports by fetching xml from msn and storing data"
def handle_noargs(self, **options):
tree = ElementTree()
path = urllib2.urlopen('http://weather.service.msn.com/data.aspx?src=vista&weadegreetype=C&culture=en-US&wealocations=wc:sfxx0010')
tree = ET.parse(path)
root = tree.getroot()
xmllist=[]
a = 0
for i in root:
xmllist = xmllist + [XmlDictConfig(i)]
for i in xmllist:
date = fixdate(i['date']) #Date, date=date heading=i['heading']
a = Forecast.objects.create(mintemp=i['low'] ,maxtemp=i['high'],wind=i['wind'],rain=i['precipitation'],date=date)
a.save()
print "Data Updated"
我得到一个键错误
KeyError:“date”我设法解决了这个问题,花了一个不眠之夜,但它就像下面的代码一样简单
class Command(NoArgsCommand):
help = "This will update the weather reports by fetching xml from msn and storing data"
def handle_noargs(self, **options):
tree = ElementTree()
path = urllib2.urlopen('http://weather.service.msn.com/data.aspx?src=vista&weadegreetype=C&culture=en-US&wealocations=wc:sfxx0010')
tree = ET.parse(path)
root = tree.getroot()
xmllist=[]
a = 0
for i in root:
xmllist = xmllist + [XmlDictConfig(i)]
for i in xmllist:
date = fixdate(i['date']) #Date, date=date heading=i['heading']
a = Forecast.objects.create(mintemp=i['low'] ,maxtemp=i['high'],wind=i['wind'],rain=i['precipitation'],date=date)
a.save()
print "Data Updated"
class命令(NoArgsCommand):
help=“这将通过从msn获取xml并存储数据来更新天气报告”
def手柄(自身,**选项):
path=urlib2.urlopen('http://weather.service.msn.com/data.aspx?src=vista&weadegreetype=C&culture=en-US&WEA位置=wc:sfxx0010')
tree=ET.parse(路径)
root=tree.getroot()
x=root.findall('*/forecast')
对于x中的预测:
日期=预测。获取('date')#日期,日期=日期航向=i['heading'],最大温度=i['high'],风=i['wind'],雨=i['prediction']
maxtemp=forecast.get('high')
wind=forecast.get('skytextday')
雨=预测。获取('precip')
mintemp=forecast.get('low')
a=预测.objects.create(mintemp=mintemp,maxtemp=maxtemp,date=date,wind=wind,rain=rain)
a、 保存()
打印“数据更新,谢谢”
您从哪里得到密钥错误?什么是回溯?嗨,丹尼尔,基本上我得到了一个关键错误:这是回溯时的错误(最近一次呼叫最后):。。。output=self.handle(*args,**选项)文件“/usr/lib/python2.6/site packages/django/core/management/base.py”,第351行,在handle-return-self.handle\u-noargs(**options)文件“/home2/saywow/bubi11/list/management/commands/weather.py”中,第120行,在handle\u-noargs-date=fixdate(i['date'])']),date=date-heading=i['heading']KeyError:“日期”