比较datetime的对象-蟒蛇

0

的问题

我有一个数据文件,该文件如下:

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。

我怎么能确定的价值观留的日期时间。时间和我可以对它们进行比较。

感谢

datetime openpyxl python
2021-11-24 00:38:46
1

最好的答案

0

你可以检查的数据类型细胞之前你比较。 还有,你会需要仔细思考比较和秩序的行删除,因为你 不能 这样做的话,你去。 而你将需要创建一个列表中的行为删除,并删除它们的顺序相反。

像这样的东西应该的工作。

import datetime

previous_date = datetime.date(2017, 12, 31) # adjust as necessary
rows_to_delete = []

for row in ws.iter_rows(min_col=1, max_col=1, min_row=2):
    cell = row[0]
    if not isinstance(cell.value, datetime.date):
        continue
    if cell.value < previous_date:
        rows_to_delete.append(cell.row)
    previous_date = cell.value

for row in reversed(rows_to_delete):
   ws.delete_rows(row)
2021-11-25 10:38:07

其他语言

此页面有其他语言版本

Русский
..................................................................................................................
Italiano
..................................................................................................................
Polski
..................................................................................................................
Română
..................................................................................................................
한국어
..................................................................................................................
हिन्दी
..................................................................................................................
Français
..................................................................................................................
Türk
..................................................................................................................
Česk
..................................................................................................................
Português
..................................................................................................................
ไทย
..................................................................................................................
Español
..................................................................................................................
Slovenský
..................................................................................................................