【发布时间】:2017-11-25 10:59:11
【问题描述】:
我在自动导入 csv 和创建 pandas 数据框时遇到问题。我得到的代码:
from datetime import time
from datetime import date
from datetime import datetime
import os
import fnmatch
def get_local_file(pdate, hour, path='/apps/dev_data/data/'):
"""Get date+hour processing file from local drive
:param pdate: str Processing date
:param hour: str Processing hour
:param path: str Path to file location
:return: Pandas DF Retrieved DataFrame
"""
sdate = pdate + '-' + str(hour)
for p_file in os.listdir(path):
if fnmatch.fnmatch(p_file, 'RSRAN098_IP_R*'+sdate+'*.csv'):
return path+p_file
def get_files(pdate, path='/apps/dev_data/data/'):
hours = [time(i).strftime('%H') for i in range(24)]
fileList=[]
for hour in hours:
fileList.append(get_local_file(pdate, hour))
return fileList
processing_date = datetime.strptime('20170614', '%Y%m%d').date()
a = get_files(str(processing_date).replace('-', '_'))
print a
frame = pd.DataFrame()
list_ = []
for file_ in a:
df = pd.read_csv(file_,index_col=None, header=0, delimiter=';')
list_.append(df)
frame = pd.concat(list_)
唯一的问题是我有一个固定的日期,我找不到放当前日期的方法,
【问题讨论】:
-
当前日期是什么意思?是今天的日期吗?
-
@AkshayKandul 是的。
-
您可以使用 datetime.now().date() 代替 datetime.strptime('20170614', '%Y%m%d').date()。这将为您提供当前日期或今天的日期。
标签: python csv pandas timestamp