Python 日期错误
Python Date Error
这是我的日志文件的几行
10/21/2015 10:16:42 AM Following hmac:c35330404902c0b1bb5c6d0718407ea12b25a464433bd1e69152ccc0e0b89c9f with is already in database so dropping
11/21/2015 10:16:42 AM The data for the duplicate Hmac is : HF 13300100012015-06-15 19:30:21+0000+ 12.61 0.010 1686.00
07/21/2015 10:16:42 AM Following hmac:84d9cdb2145b7c3e0fa2d099070b7bd291c652f30ca25c69240e33ebbd2b8677 with is already in database so dropping
07/21/2016 10:16:42 AM The data for the duplicate Hmac is : HF 13300100012015-06-15 20:16:18+0000+ 12.60 0.045 1686.00
07/20/2016 10:16:42 AM Following hmac:a24d19d340651e694bff854ae7469dd779b60037228bf047d8f372dee4a731e0 with is already in database so dropping
07/20/2016 10:16:42 AM The data for the duplicate Hmac is : HF 13300100012015-06-15 20:31:25+0000+ 12.62 0.045 1685.00
07/20/2016 10:16:42 AM Following hmac:4e239a4b69108833e9cbc987db2014f9137679860df0ca8efdf7d09c4897d369 with is already in database so dropping
07/19/2016 10:16:42 AM The data for the duplicate Hmac is : HF 13300100012015-06-15 20:46:27+0000+ 12.61 0.040 1685.00
我的目标是遍历行和 return 行计数特定字符,包括 hmac。我已经计算了总计数,但我想 return 过去一年的行数。尝试提取每一行的日期部分给我一个错误
ValueError:未转换的数据仍然存在:
我试过但找不到解决方案。这是我的代码
从日期时间导入日期
从日期时间导入时间
从日期时间导入日期时间
从日期时间导入时间增量
导入 os
def fileCount(fileName):
with open(fileName) as FileObj:
Count = 0
todayDate = date.today()
OneYear = str(todayDate - timedelta(days=365))
OneMonth = str(todayDate - timedelta(days=30))
ThreeMonths = str(todayDate - timedelta(days=90))
while True:
line = FileObj.readline()
Lines = "-".join(line[:11].split("/"))
convertDate = datetime.strptime(Lines, '%m-%d-%Y')
print convertDate
if not line:
break
if "Following hmac" in line:
Count += 1
print "The total count is ", Count
# Call The function
def main():
filePath = 'file.txt'
fileCount(filePath)
if __name__ == "__main__":
main()
我想提取日期以将其用于日期算术运算,这样我就可以 return 计算过去三个月、六个月和 12 个月的数据。
您的切片的停止索引包括尾随 space,您提供的日期格式中未考虑该索引。
你应该去掉 space:
>>> datetime.strptime(Lines.rstrip(), '%m-%d-%Y')
datetime.datetime(2015, 10, 21, 0, 0)
或将索引更改为 10
而不是 11
以完全排除 space:
Lines = "-".join(line[:10].split("/"))
考虑格式中的额外 space 也是另一个修复方法:
convertDate = datetime.strptime(Lines, '%m-%d-%Y ')
您可以使用 try/except
:
处理其他错误,例如空白行或没有日期字符串的行
lines = "-".join(line[:10].split("/"))
try:
convert_date = datetime.strptime(lines, '%m-%d-%Y')
print convert_date
except ValueError:
print 'This line has a problem:', lines
这是我的日志文件的几行
10/21/2015 10:16:42 AM Following hmac:c35330404902c0b1bb5c6d0718407ea12b25a464433bd1e69152ccc0e0b89c9f with is already in database so dropping
11/21/2015 10:16:42 AM The data for the duplicate Hmac is : HF 13300100012015-06-15 19:30:21+0000+ 12.61 0.010 1686.00
07/21/2015 10:16:42 AM Following hmac:84d9cdb2145b7c3e0fa2d099070b7bd291c652f30ca25c69240e33ebbd2b8677 with is already in database so dropping
07/21/2016 10:16:42 AM The data for the duplicate Hmac is : HF 13300100012015-06-15 20:16:18+0000+ 12.60 0.045 1686.00
07/20/2016 10:16:42 AM Following hmac:a24d19d340651e694bff854ae7469dd779b60037228bf047d8f372dee4a731e0 with is already in database so dropping
07/20/2016 10:16:42 AM The data for the duplicate Hmac is : HF 13300100012015-06-15 20:31:25+0000+ 12.62 0.045 1685.00
07/20/2016 10:16:42 AM Following hmac:4e239a4b69108833e9cbc987db2014f9137679860df0ca8efdf7d09c4897d369 with is already in database so dropping
07/19/2016 10:16:42 AM The data for the duplicate Hmac is : HF 13300100012015-06-15 20:46:27+0000+ 12.61 0.040 1685.00
我的目标是遍历行和 return 行计数特定字符,包括 hmac。我已经计算了总计数,但我想 return 过去一年的行数。尝试提取每一行的日期部分给我一个错误
ValueError:未转换的数据仍然存在:
我试过但找不到解决方案。这是我的代码 从日期时间导入日期 从日期时间导入时间 从日期时间导入日期时间 从日期时间导入时间增量 导入 os
def fileCount(fileName):
with open(fileName) as FileObj:
Count = 0
todayDate = date.today()
OneYear = str(todayDate - timedelta(days=365))
OneMonth = str(todayDate - timedelta(days=30))
ThreeMonths = str(todayDate - timedelta(days=90))
while True:
line = FileObj.readline()
Lines = "-".join(line[:11].split("/"))
convertDate = datetime.strptime(Lines, '%m-%d-%Y')
print convertDate
if not line:
break
if "Following hmac" in line:
Count += 1
print "The total count is ", Count
# Call The function
def main():
filePath = 'file.txt'
fileCount(filePath)
if __name__ == "__main__":
main()
我想提取日期以将其用于日期算术运算,这样我就可以 return 计算过去三个月、六个月和 12 个月的数据。
您的切片的停止索引包括尾随 space,您提供的日期格式中未考虑该索引。
你应该去掉 space:
>>> datetime.strptime(Lines.rstrip(), '%m-%d-%Y')
datetime.datetime(2015, 10, 21, 0, 0)
或将索引更改为 10
而不是 11
以完全排除 space:
Lines = "-".join(line[:10].split("/"))
考虑格式中的额外 space 也是另一个修复方法:
convertDate = datetime.strptime(Lines, '%m-%d-%Y ')
您可以使用 try/except
:
lines = "-".join(line[:10].split("/"))
try:
convert_date = datetime.strptime(lines, '%m-%d-%Y')
print convert_date
except ValueError:
print 'This line has a problem:', lines