![](/img/trans.png)
[英]IndexError : list index out of range when retrieving text from a file
[英]IndexError: List Index out of Range when grabbing gmail text
我正在使用此代码登录到我的Gmail帐户并从Steampowered的电子邮件中检索文本。
else:
g = gmail.login(emailAddress, emailPassword)
# Check for emails until we get Steam Guard code
for i in range(0, 3):
mails = g.inbox().mail(sender="noreply@steampowered.com", unread=True)
if mails:
mail = mails[-1]
mail.fetch()
#print re.findall(r"you'll need to complete the process: ([A-Z0-9]{5})", mail.body)
guardCode = re.findall(r"you'll need to complete the process: ([A-Z0-9]{5})", mail.body)[0]
mail.read()
mail.delete()
g.logout()
return guardCode
else:
time.sleep(3)
我得到一个
IndexError:列表索引超出范围
在这一行:
guardCode = re.findall(r"you'll need to complete the process: ([A-Z0-9]{5})", mail.body)[0]
这是因为
re.findall(r"you'll need to complete the process: ([A-Z0-9]{5})", mail.body)
退货
[]
因此列表为空。 为什么它是空的?
这是电子邮件的格式:
您是否正在使用新的浏览器或Steam应用程序登录Steam? 这是完成该过程所需的Steam Guard代码:XXXXX <----我需要这个
编辑: Gmail是从此处开始的自定义python模块。 编辑2:这是电子邮件的更好表示:
尊敬的XXX,
您是否正在使用新的浏览器或Steam应用程序登录Steam? 这是完成该过程所需的Steam Guard代码:
XXXXX
如果您最近尚未尝试从位于XXXX(美国)的设备登录Steam,则可能是其他人正在尝试访问您的帐户。 您可以在线查看有关此登录尝试的更多信息。
如果您怀疑其他人可能正在尝试访问您的帐户,请执行以下任一操作:
问题是因为'
在you'll
。 这不是普通的单引号'
。
>>> s = "Are you logging into Steam using a new browser or Steam app? Here’s the Steam Guard code you’ll need to complete the process: XXXXX"
>>> re.findall(r"you’ll need to complete the process: ([A-Z0-9]{5})", s)
['XXXXX']
我解决了我的问题。 我刚刚删除了所有换行符,并将电子邮件正文文本作为一个完整的字符串。 感谢所有提供帮助的人!
str = re.sub('\s+',' ', mail.body)
guardCode = re.findall(r'to complete the process: ([A-Z0-9]{5})', str)[0]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.