我有一个名为C:\ Flows的文件夹,在其中,今年一整天都有20190101_Flows.csv
格式的多个文件。
我想每天导入一个文件,然后对其执行操作,然后保存该文件。但是,我坚持下去。
到目前为止,我已经编写了以下代码来获取文件。但是之后我被困住了。
from datetime import date
from datetime import datetime, timedelta
import glob
from os import path
DATE_FORMAT = "%Y%m%d"
pathDir = r'C:/Flows/'
dateStart = "20190301"
dateEnd = "20190305"
start_date = datetime.strptime(dateStart, DATE_FORMAT).date()
end_date = datetime.strptime(dateEnd, DATE_FORMAT).date()
delta_one_day = timedelta(days=1)
date = start_date
while date <= end_date:
data_folder = path.join(pathDir)
if path.isdir(data_folder):
for filename in glob(os.path.join(data_folder, "*_Flows.csv")):
#this is where i would like to perform the operations on the file imported
df['code']=df[df.columns[1]].astype(str).str[0:3]
df['bmunit']=df[df.columns[1]].astype(str).str[4:]
df['checko']=df.iloc[:,2:50].sum(axis=1)
print filename
date += delta_one_day