我正在使用dbf模块。我需要搜索(查询)发票日期在 2023-01-23 到 2023-01-25 之间的所有产品,包括这两个日期。
我有 dbf 表(product.dbf),其中包含以下数据
invno invdt hsn mname packg
2010 2023-01-21 30086666 Medicine01 10'S
2010 2023-01-23 30044444 Medicine02 10'S
2010 2023-01-23 30043333 Medicine03 15'S
2010 2023-01-23 30049999 Medicine04 15'S
2011 2023-01-24 30049999 Medicine05 15'S
2011 2023-01-24 30049999 Medicine06 15'S
2011 2023-01-24 30049444 Medicine07 30'ML
2011 2023-01-24 30049333 Medicine08 30'ML
2011 2023-01-26 30049111 Medicine09 30'ML
2011 2023-01-26 30049111 Medicine09 30'ML
2012 2023-01-31 30042333 Medicine10 60'S
2012 2023-01-31 30042234 Medicine11 15'S
我正在尝试使用下面的代码,它没有按预期工作。 当表中没有确切日期时,我遇到异常。例如,如果 25-01-2023 不存在,则将显示以下异常
dbf.NotFoundError: 'dbf.Index.index_search(x): x 不在索引中'
import dbf
import datetime
table = dbf.Table('product.dbf')
table.open(dbf.READ_ONLY)
index = table.create_index(lambda rec: rec.invdt)
# search between 2023-01-23 to 2023-01-25, include both dates.
fromdate = index.index_search(match=(datetime.datetime.strptime('23-01-2023','%d-%m-%Y').date()))
todate = index.index_search(match=(datetime.datetime.strptime('25-01-2023','%d-%m-%Y').date()))
for row in index[fromdate:todate]:
print(row[0],row[1],row[2],row[3],row[4])
table.close()
您想比较 2023 年 1 月 25 日和 2023 年 1 月 25 日吗?
这样比较会更容易
desiredRange = table.loc[(table['invdt'] >= start_date) & (table['invdt'] <= end_date)]