久久精品国产亚洲高清|精品日韩中文乱码在线|亚洲va中文字幕无码久|伊人久久综合狼伊人久久|亚洲不卡av不卡一区二区|精品久久久久久久蜜臀AV|国产精品19久久久久久不卡|国产男女猛烈视频在线观看麻豆

    1. <style id="76ofp"></style>

      <style id="76ofp"></style>
      <rt id="76ofp"></rt>
      <form id="76ofp"><optgroup id="76ofp"></optgroup></form>
      1. 千鋒教育-做有情懷、有良心、有品質的職業(yè)教育機構

        手機站
        千鋒教育

        千鋒學習站 | 隨時隨地免費學

        千鋒教育

        掃一掃進入千鋒手機站

        領取全套視頻
        千鋒教育

        關注千鋒學習站小程序
        隨時隨地免費學習課程

        當前位置:首頁  >  技術干貨  > python輸入數(shù)字變成月份

        python輸入數(shù)字變成月份

        來源:千鋒教育
        發(fā)布人:xqq
        時間: 2023-11-11 22:48:44 1699714124

        python輸入數(shù)字變成月份

        1、思路說明

        可計算給定區(qū)間的時間差,即兩者之間共包含幾個月。然后由第一個月(開始時間)逐漸累積,最后得到給定時間區(qū)間所有月份的清單。

        2、時間差計算:我們可以使用第三方庫dateutil中的rrule.count函數(shù)來實現(xiàn)。

        Importdatetimefromdateutilimportrrule

        start=datetime.datetime.strptime('2019.01','%Y.%m')

        end=datetime.datetime.strptime('2019.05','%Y.%m')print(start.month)

        rrule.rrule(rrule.MONTHLY,dtstart=start,until=end).count()

        3、每月累積計算:在這里,我們可以使用for循環(huán)和range()函數(shù),根據(jù)總月數(shù),逐步累積,例如:2019.01-2019.05共5個月,從0到4迭代,從1+0=1到1+4=5,就可以得到所有月份;此外,當月迭代累積結果超過12時,將累積結果除以12取余,并將年份加1,就可以得到正確的年月時間。

        importdatetimefromdateutilimportrruledefget_each_month(start_month,end_month):ifstr(start_month).count('.')!=1orstr(end_month).count('.')!=1:print("ParameterError:Plsinputastringsuchas'2019.01'")return[]ifint(str(start_month).split('.')[1])>12orint(str(end_month).split('.')[1])>12:print('ParameterError:Plsinputcorrectmonthrangesuchasbetween1to12')return[]ifint(str(start_month).split('.')[1])==0orint(str(end_month).split('.')[1])==12:print('ParameterError:Plsinputcorrectmonthrangesuchasbetween1to12')return[]

        start=datetime.datetime.strptime(start_month,"%Y.%m")

        end=datetime.datetime.strptime(end_month,"%Y.%m")

        month_count=rrule.rrule(rrule.MONTHLY,dtstart=start,until=end).count()#計算總月份數(shù)

        ifend

        list_month=[]

        year=int(str(start)[:7].split('-')[0])#截取起始年份

        forminrange(month_count):#利用range函數(shù)填充結果列表

        month=int(str(start)[:7].split('-')[1])#截取起始月份,寫在for循環(huán)里,作為每次迭代的累加基數(shù)

        month=month+mifmonth>12:ifmonth%12>0:

        month=month%12#計算結果大于12,取余數(shù)

        ifmonth==1:

        year+=1#只需在1月份的時候對年份加1,注意year的初始化在for循環(huán)外

        else:

        month=12

        iflen(str(month))==1:

        list_month.append(str(year)+'.0'+str(month))else:

        list_month.append(str(year)+'.'+str(month))returnlist_month

        以上就是python輸入數(shù)字變成月份的方法,基本的流程分享給大家,看懂后可以進行實例部分的嘗試。更多Python學習教程請關注IT培訓機構:千鋒教育。

        tags: python培訓
        聲明:本站稿件版權均屬千鋒教育所有,未經(jīng)許可不得擅自轉載。
        10年以上業(yè)內強師集結,手把手帶你蛻變精英
        請您保持通訊暢通,專屬學習老師24小時內將與您1V1溝通
        免費領取
        今日已有369人領取成功
        劉同學 138****2860 剛剛成功領取
        王同學 131****2015 剛剛成功領取
        張同學 133****4652 剛剛成功領取
        李同學 135****8607 剛剛成功領取
        楊同學 132****5667 剛剛成功領取
        岳同學 134****6652 剛剛成功領取
        梁同學 157****2950 剛剛成功領取
        劉同學 189****1015 剛剛成功領取
        張同學 155****4678 剛剛成功領取
        鄒同學 139****2907 剛剛成功領取
        董同學 138****2867 剛剛成功領取
        周同學 136****3602 剛剛成功領取
        相關推薦HOT
        凤山县| 阿城市| 福州市| 田林县| 阿尔山市| 三江| 邯郸县| 洪雅县| 景泰县| 漳州市| 阳西县| 德安县| 繁昌县| 海口市| 东港市| 额尔古纳市| 巴彦淖尔市| 镇安县| 广饶县| 榆林市| 绥德县| 新和县| 无为县| 长宁区| 剑河县| 仁布县| 鄂伦春自治旗| 牟定县| 三台县| 文登市| 朝阳县| 申扎县| 许昌市| 大同市| 建水县| 呼和浩特市| 手机| 塘沽区| 八宿县| 洪湖市| 利辛县|