【发布时间】:2020-07-14 15:52:30
【问题描述】:
我正在尝试编写一个代码,它从以“From”开头的行中提取时间码。示例:“From stephen.marquard@uct.ac.za Sat Jan 5 09:14:16 2008”,然后将时间码拆分为小时和秒。
fhand = open('mbox-short.txt')
for line in fhand :
line = line.rstrip()
if not line.startswith('From') : continue
words = line.split()
time = words[5:6]
hrs = time.split(':')
print(hrs[1])
print(hrs[2])
当我编译我的代码时 - 我得到了回溯(属性错误:'list' object has no attribute 'split')。如果我更改我的代码以对电子邮件执行相同的操作:
fhand = open('mbox-short.txt')
for line in fhand :
line = line.rstrip()
if not line.startswith('From') : continue
words = line.split()
time = words[1]
hrs = time.split('@')
print(hrs[1])
一切正常 - 程序正常运行(将电子邮件拆分为登录名和域)。第一个代码有什么问题?
【问题讨论】:
-
切片
words[5:6]会返回一个列表,即使里面只有一件事
标签: python list split attributes traceback