我有一个数据文件,该文件如下:
Date item purchased
01-12-2018 Car
02-12-2018 Truck
03-12-2018 Car
04-12-2018 Bike
作为一部分的数据清理过程中,我需要检查的数据是按时间顺序排列。 所以我需要检查的日期在一个行之日后在前列。 如果没有,我需要删除该行。 我是指没有使用大熊猫的图书馆。
迄今为止,我已经做了下列步骤:
#If the file name is - 'Input_file'
from openpyxl import load_workbook
from datetime import datetime
#Reading the file
wb = load_workbook(Input_file)
sheet = wb.active
#Reading the Date column in the file
Date_column = sheet['A']
#Reading each row and the date in each row to compare it with the previous row date
for x in range(len(Date_column)):
Datenow = Date_column[x].value
Datebef= Date_column[x-1].value
Check = Datenow > Datebef
print(Check)
错误的时候我尝试比较datetime对象是:
TypeError: unsupported operand type(s) for -: 'str' and 'datetime.datetime'
问题是,当我检查的类型将日期列,它出来是日期时间。datetime但只要我尝试比较这两个日期时间。datetime对象,它告诉我,一个是串的和一日期时间。datetime对象。 的混淆是如果这两个价值观正在读取相同的柱,一个是如何出来作为串的和一日期时间。datetime。
我怎么能确定的价值观留的日期时间。时间和我可以对它们进行比较。
感谢