我在同一路径中有 2 个 excel 文件。对于第一个 excel,我为用户提供了一个输入。当他们输入年-月-日时,它发生了 20210830(示例)。正如你在下面看到的,我合并了用户的输入和固定的“dh”和固定的“路径”。如果此路径包含此文件夹名称,文件将打开。
我真正的问题是关于“第二个 Excel”。我想要的是当用户输入第一个 excel 的日期时,它将通过将同一文件路径中的第二个 excel 的名称和该日期之前的一个工作日组合起来来搜索文件
#first_excel = dh20210830
#second_excel = Test_Value_Serie_After_20210827_123456
path = K:\Test\
suffix = dh
final = path + dh + date
data = pd.read_csv(final,sep=';',encoding='utf-8')
df = pd.DataFrame(data)
add_year = str(input("Year:"))
add_month = str(input("Month:"))
add_day = str(input("Day:"))
date = add_year + add_month + add_day
suffix_second = Test_Value_Serie_After_
final_second = suffix_second + """""one business day before the user enters""""" + _123456 (But this number can be variable. So don't rely on these numbers. If there are different numbers, it may be in that file.)
constant variables = Test_Value_Serie_After_
path = K:\Test\
variable things = date(users input) and _123456(this number can be any number)
注意:正如我告诉你的,在最后一个“_”之后,无论后面的数字如何,都打开文件。
预期输出:
suffix_second = Test_Value_Serie_After_
date_second = date - 1 business day (in this example 20210827)...2 day is weekend)
final_second = suffix_second + date_second + '_'(open the file regardless of the numbers after it)
pd.tseries.offsets.BusinessDay
如果您有特定的假期等,请使用或使用自定义工作日。
add_year = str(input("Year:"))
add_month = str(input("Month:"))
add_day = str(input("Day:"))
date = add_year + add_month + add_day
# convert date to datetime then offset 1 business day
date_offset = pd.to_datetime(date) - pd.tseries.offsets.BusinessDay(1)
print(date_offset)
Year: 2021
Month: 08
Day: 30
2021-08-27 00:00:00
然后您可以将日期转换回字符串
str_date_offset = str(date_offset.date()).replace('-', '') # -> '20210827'
用户glob
匹配文件名的模式
import glob
file_name = glob.glob(f'{suffix_second}{str_date_offset}_*.csv')[0]
print(file_name)
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句