在Python中将“0000-00-00”转换为“标志”

我想在一个特定的范围内检查一些date的数据转换成string。 我首先将所有数据转换为floattypes,以便它可以提供date格式的输出,但是当我将其应用于显示的date时:

a1 = float(a1) ValueError: could not convert string to float: '0000-00-00' 

我的整个代码是:

 import xlrd import os.path from datetime import datetime date_array = [] wb = xlrd.open_workbook(os.path.join('E:\Files','SummaryLease.xlsx')) sh = wb.sheet_by_index(0) for i in range(1,sh.nrows): a1 = sh.cell_value(rowx=i, colx=80) if a1 is '0000-00-00': date_array.append('flag') else: a1 = float(a1) a1_as_datetime = datetime(*xlrd.xldate_as_tuple(a1, wb.datemode)) date_array.append(a1_as_datetime.date()) print(date_array) 

我应该如何解决这个问题?

不要使用is运算符来比较string,请使用==

 if a1 == '0000-00-00': date_array.append('flag') else: a1 = float(a1) 

你可以阅读更多关于这里的区别: 在Python中`==`和`is`是否有区别?