Python .split IndexError [英] Python .split IndexError

查看:37
本文介绍了Python .split IndexError的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Python的新手.尝试调用.split来分隔时间值,这样我就可以计算出4月的所有登录日期:如果我调用response ['data]

New to Python. Trying to call .split to seperate time values so I can count all the login dates for april: here is what the data looks like if I call response['data]

[{u'email': u'wilderman.nyree@heidenreich.com'}, {u'login_date': u'2014-05-04T04:09:29-07:00', u'email': u'vrippin@gmail.com'}, {u'login_date': u'2014-04-13T20:37:31-04:00', u'email': u'eileen.gusikowski@oreilly.info'}, {u'login_date': u'2014-05-11T15:34:17-04:00', u'email': u'cstehr@keebler.com'}, {u'login_date': u'2014-04-13T17:12:20-04:00', u'email': u'onolan@gmail.com'}, {u'login_date': u'2014-04-01T02:22:55+05:00', u'email': u'ygoyette@yahoo.com'}, {u'login_date': u'2014-04-22T21:04:03+04:00', u'email': u'vblanda@gmail.com'}, {u'login_date': u'2014-05-08T22:30:57-04:00', u'email': u' ybartoletti@littel.biz'}, {u'login_date': u'2014-04-25T13:27:48-08:00', u'email': u'woodie.crooks@kozey.com'}, {u'login_date': u'2014-04-14T02:18:48-07:00', u'email': u'emilia16@block.com'}, {u'login_date': u'2014-04-20T11:02:21-10:00', u'email': u'bahringer.karl@yahoo.com'}, {u'login_date': u'2014-04-27T11:08:50+10:00', u'email': u'morissette.lalla@kuhicmuller.com'}]

依此类推...当我运行这段代码时:

And so on and so forth...when I run this code:

 loggedList = [user['login_date'] for user in response['data']]
 for login in loggedList:
    if login is not None:
     aprilList.append('-' + login.split('-')[0])


 # print aprilList
 print response['data']

我在控制台中得到此打印输出:

I get this printout in the console:

u'-2014', u'-2014', u'-2014', u'-2014', u'-2014', u'-2014', u'-2014', u'-2014', u'-2014', u'-2014', u'-2014', u'-2014', u'-2014', u'-2014', u'-2014', u'-', u'-2014', u'-2014', u'-2014', u'-2014', u'-2014', u'-2014', u'-2014', u'-2014', u'-2014', u'-2014', u'-2014', u'-2014', u'-2014', u'-2014']

但是当我尝试通过将 login.split('-')[0])更改为 login.split('-')来访问.split列表的第二部分时[1])我收到一个IndexError列表超出范围.

But when I try and access the second part of the .split list by changing login.split('-')[0])to login.split('-')[1]) I get an IndexError list is out of range.

如何访问登录日期的月份部分?我想我已经快要用.split方法隐藏了.

How do I access the month part of the login dates? I think I'm close and it's hiding somewhere with the .split method.

推荐答案

如果查看 login.split('-')[0])的输出,您会注意到一个是 u'-',

If you look through your output for login.split('-')[0]), you'll notice that one is u'-',

这可能意味着您的一个数据条目的登录日期为空,因此login.split给出的数组中仅包含空字符串.它有1个元素,所以第二个索引超出范围

This likely means that one of your data entries has an empty login date, and so login.split is giving an array with just the empty string in it. This has 1 element, so the second index is out of bounds

要么确保您所有数据的格式正确且完整,要么仅在login.split结果足够长时尝试打印月份

Either ensure all your data is of the correct format and complete, or only try to print the month if the result of login.split is long enough

这篇关于Python .split IndexError的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆