SMN*_*LLY 0 python for-loop date monthcalendar python-2.7
我试图从2012年和11月开始,一直持续几年的月份列表,一直到当前年份和月份.
start_year = "2012"
start_month = "November"
month = start_month
year = start_year
# some sort of for loop or cycle here
try:
with open('{} {}.csv'.format(month, year)):
CSVFile = True
if CSVFile:
print "do something cool here"
except IOError:
CSVFile = False
print ""
Run Code Online (Sandbox Code Playgroud)
任何有关如何获得此建议或建议的建议或建设性意见将不胜感激.谢谢SMNALLY
我可能会这样做如下:
import datetime, os
current_month = datetime.date.today().replace(day=1)
# this could be more concise if `start_month` were the month number rather than month name
possible_month = datetime.datetime.strptime('%s %s' % (start_month, start_year), '%B %Y').date()
while possible_month <= current_month:
csv_filename = possible_month.strftime('%B %Y') + '.csv'
if os.path.exists(csv_filename):
CSVFile = True
# do something cool here, and maybe break the loop if you like
possible_month = (possible_month + datetime.timedelta(days=31)).replace(day=1)
Run Code Online (Sandbox Code Playgroud)
如果您需要我扩展它的工作方式,请告诉我.
| 归档时间: |
|
| 查看次数: |
96 次 |
| 最近记录: |