TypeError:'NoneType'对象不可订阅?

时间:2015-10-27 17:26:22

标签: python

我收到此错误,不知道问题是什么

__author__ = 'victor'

import requests
import xmltodict
url = 'http://webservices.ns.nl/ns-api-storingen?station=UT'
user = 'victor_shao@hotmail.com'
passw = 'dMHAg67WACsUdrQmqctr3giaTpTnsJmhPP2EcQWlVnMw3zmHXvSE-A'
response = requests.get(url, auth=(user, passw))

xmldi = xmltodict.parse(response.text)

storing = 0
allestoringen = 4

print("Storingen",'\n','ongeplande storingen')
print('{0:15s}'.format('id'),'{0:40s}'.format('traject'),'{0:20s}'.format('reden'),'{0:100s}'.format('bericht'),'{0:25s}'.format('datum'))
for storing in range(0, allestoringen):
    id = xmldi['Storingen']['Ongepland']['Storing']['id']
    traject = xmldi['Storingen']['Ongepland']['Storing']['Traject']
    reden = xmldi['Storingen']['Ongepland']['Storing']['Reden']
    bericht = xmldi['Storingen']['Ongepland']['Storing']['Bericht']
    datum = xmldi['Storingen']['Ongepland']["Storing"]['Datum']

print("Geplande storingen ")
print('{0:25s)'.format('id'),'{0:40s}'.format('traject'),'{0:35s}'.format('periode'),'{0:200s}'.format('advies'),'{0:250s}'.format('bericht'),'{0:50s}'.format('oorzaak'),'{0:30s}'.format('vertraging'))
for storing in range(0, allestoringen):
    id = xmldi['Storingen']['Gepland']['Storing']['id']
    traject = xmldi['Storingen']['Gepland']['Storing']['Traject']
    periode = xmldi['Storingen']['Gepland']['Storing']['Periode']
    advies = xmldi['Storingen']['Gepland']['Storing']['Advies']
    bericht = xmldi['Storingen']['Gepland']['Storing']['Bericht']
    oorzaak = xmldi['Storingen']['Gepland']['Storing']['Oorzaak']
    vertraging = xmldi['Storingen']['Gepland']['Storing']['Vertraging']

这是输出,我应该填充信息:

C:\Python34\python.exe C:/Users/avi/PycharmProjects/untitled1/storing.py

Storingen  ongeplande storingen id traject reden bericht datum
Traceback(最近一次调用最后一次):   文件“C:/Users/avi/PycharmProjects/untitled1/storing.py”,第18行,in     id = xmldi ['Storingen'] ['Ongepland'] ['Storing'] ['id'] TypeError:'NoneType'对象不可订阅

使用退出代码1完成处理

1 个答案:

答案 0 :(得分:0)

   since Ongepland has as value only None

   id = xmldi['Storingen']['Ongepland']['Storing']['id']

   should be changed to: 


   id = xmldi['Storingen']['Gepland']['Storing']['id']