V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
推荐学习书目
Learn Python the Hard Way
Python Sites
PyPI - Python Package Index
http://diveintopython.org/toc/index.html
Pocoo
值得关注的项目
PyPy
Celery
Jinja2
Read the Docs
gevent
pyenv
virtualenv
Stackless Python
Beautiful Soup
结巴中文分词
Green Unicorn
Sentry
Shovel
Pyflakes
pytest
Python 编程
pep8 Checker
Styles
PEP 8
Google Python Style Guide
Code Style from The Hitchhiker's Guide
princelai
V2EX  ›  Python

有没有 python3 可用的农历转公历的包?

  •  1
     
  •   princelai · 2017-01-06 19:11:26 +08:00 · 3840 次点击
    这是一个创建于 2637 天前的主题,其中的信息可能已经有所发展或是发生改变。

    我想写个小脚本,内容是把一年中是周末且周末是在新月那几天(农历 27-3 日),但是现在找不到公历转农历的包,之前的工作如下:

    #生成一个一年的日历

    df = pd.DataFrame(pd.date_range(start='2017-01-01',end='2017-12-31'),columns=['公历'])

    df = df.set_index(keys='公历')

    #找出所有周末

    df['星期几'] = df.index.weekday + 1

    Sat = df['星期几'] == 6

    Sun = df['星期几'] == 7

    #找出国庆节,别的节太短或者和周末都挨着

    National_Day = df['2017-10-01':'2017-10-07']

    new_df = pd.concat([df[Sat | Sun],National_Day])

    df2 = df.ix[new_df.sort_index().index.drop_duplicates()]

    下一步就该找出这个日历的农历了,有没有人教教我怎么做?

    pypi 上有个 lunarsolarcovter 是 py2 用的,我用不了

    5 条回复    2018-08-09 10:09:32 +08:00
    billlee
        1
    billlee  
       2017-01-06 20:22:45 +08:00   ❤️ 1
    LunarSolarConverter 就 200 行代码,你自己改一下不就完了
    imn1
        2
    imn1  
       2017-01-06 21:10:33 +08:00   ❤️ 1
    标题是农转公,正文是公转农,要哪个?


    !/usr/bin/env python3
    # -*- coding: utf-8 -*-

    # lunar.py
    # 2015/02/27 罗兵

    google 这个能找到
    princelai
        3
    princelai  
    OP
       2017-01-06 21:16:26 +08:00
    @imn1 公转农, github 搜到了,谢谢
    princelai
        4
    princelai  
    OP
       2017-01-06 21:27:11 +08:00
    @billlee 之前只想着从 pypi 下载,没仔细看他内容,我以为会有别的时间包也能实现呢
    lifulinghan
        5
    lifulinghan  
       2018-08-09 10:09:32 +08:00
    有啊,你可以试一下 sxtwl,pip install sxtwl

    import sxtwl

    #日历中文索引
    ymc = [u"十一", u"十二", u"正", u"二", u"三", u"四", u"五", u"六", u"七", u"八", u"九", u"十" ]
    rmc = [u"初一", u"初二", u"初三", u"初四", u"初五", u"初六", u"初七", u"初八", u"初九", u"初十",
    u"十一", u"十二", u"十三", u"十四", u"十五", u"十六", u"十七", u"十八", u"十九",
    u"二十", u"廿一", u"廿二", u"廿三", u"廿四", u"廿五", u"廿六", u"廿七", u"廿八", u"廿九", u"三十", u"卅一"]


    lunar = sxtwl.Lunar() #实例化日历库

    #阴历转阳历
    day = lunar.getDayByLunar(2030, 3, 27 , False)

    print u"公历:", day.y, u"年", day.m, u"月", day.d, u"日"
    if day.Lleap:
    print u"阴历:润", ymc[day.Lmc], u"月", rmc[day.Ldi], u"日"
    else:
    print u"阴历:", ymc[day.Lmc], u"月", rmc[day.Ldi], u"日"



    #同理,阳历转阴历

    day = lunar.getDayBySolar(2018, 10, 20)
    print u"公历:", day.y, u"年", day.m, u"月", day.d, u"日"
    if day.Lleap:
    print u"阴历:润", ymc[day.Lmc], u"月", rmc[day.Ldi], u"日"
    else:
    print u"阴历:", ymc[day.Lmc], u"月", rmc[day.Ldi], u"日"
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   4802 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 25ms · UTC 09:57 · PVG 17:57 · LAX 02:57 · JFK 05:57
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.