1
這裏是我的記錄文件的某些幾行Python的日期錯誤
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
我的目標是遍歷所有的線和線的特定字符返回計數包括HMAC。我已經計算了總數,但是我想返回最近一年的行數。試圖提取每一行的日期部分是給我一個錯誤
ValueError異常:未轉換的數據仍然是:
,我都試過,但無法找到解決方案。這裏是我的代碼 從日期時間進口日期 從日期時間導入時間 從日期時間進口日期時間 從日期時間進口timedelta 進口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()
我想提取的日期使用它的日期算術運算這將允許我返回最近三個月,六個月和十二個月的計數。
@摩西Koledoye。謝謝你快速的回覆。嘗試任何建議的解決方案都會給出另一個錯誤ValueError:時間數據''與格式'%m-%d-%Y'不匹配。我正在使用python 2.7 – user1895915
文件中有空行。你只應該解析包含日期字符串的行。將這行代碼封裝在「try/except ValueError」之外。請參閱答案中的更新。 –