推荐学习书目
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
frmongo
V2EX  ›  Python

时间问题

  •  
  •   frmongo · Aug 15, 2018 · 2167 views
    This topic created in 2837 days ago, the information mentioned may be changed or developed.

    1. 调试如下代码,总是报错

    搜索了一顿,好像还是没明白出错原因

    2. CODE

    # coding=utf-8
    # python2.7
    
    from datetime import datetime
    import re
    
    t1= u"2018 年 01 月 1"
    t2= u"2018 年 02 月 22"
    t3= u"2018 年 3 月 37 日"
    
    
    
    
    def str2digi(strin):
        m = re.match(u"(\d+)年(\d+)月(\d+)日?", strin)
        
        yearn = m.group(1)
        monthn = m.group(2)
        dayn = m.group(3)
        
        
        if len(m.group(2)) == 1:
            monthn = '0'+ monthn
    
        if len(m.group(3)) == 1:
            dayn = '0' + dayn
    
        cc = yearn + monthn + dayn
        print 'firstdata is ',cc
        print [cc]
        ee = str(cc)
        print [ee]
        
        time = datetime.strptime(ee, "%Y%m%d").date()
        return time
        
    
    print 'time is [%s]' % str2digi(t1)
    print 'time is [%s]' % str2digi(t2)
    print 'time is [%s]' % str2digi(t3)
    

    3. 输出

    firstdata is  20180101
    [u'20180101']
    ['20180101']
    time is [2018-01-01]
    firstdata is  20180222
    [u'20180222']
    ['20180222']
    time is [2018-02-22]
    firstdata is  20180337
    [u'20180337']
    ['20180337']
    Traceback (most recent call last):
      File ".\t2.py", line 40, in <module>
        print 'time is [%s]' % str2digi(t3)
      File ".\t2.py", line 34, in str2digi
        time = datetime.strptime(ee, "%Y%m%d").date()
      File "C:\Python27\lib\_strptime.py", line 335, in _strptime
        data_string[found.end():])
    ValueError: unconverted data remains: 7
    
    8 replies    2018-08-15 14:11:42 +08:00
    frmongo
        1
    frmongo  
    OP
       Aug 15, 2018
    说明:t1 t2 t3 里没有空格,没有空格,这个是 V2EX 显示问题
    aijam
        2
    aijam  
       Aug 15, 2018
    3 月有 37 日?
    frmongo
        3
    frmongo  
    OP
       Aug 15, 2018
    哈哈,我就是试试,忘了这茬
    frmongo
        4
    frmongo  
    OP
       Aug 15, 2018
    我去,我真是晕了
    hahastudio
        5
    hahastudio  
       Aug 15, 2018
    话说回来,如果你真的确定你发帖的时候没有空格,可以 @ 一下 Livid,是不是 V2EX 自动给数字和中文之间加的空格,对 code block 也起作用了
    princelai
        6
    princelai  
       Aug 15, 2018
    如果你能保证都是中文的年月这种格式,我倒是有一个好办法

    ```
    import arrow

    t1= u"2018 年 01 月 1"
    t2= u"2018 年 02 月 22"
    t3= u"2018 年 3 月 27 日"
    tt = [t1,t2,t3]

    date_format = ['YYYY 年 M 月 D','YYYY 年 M 月 DD','YYYY 年 MM 月 D','YYYY 年 MM 月 DD']

    def format_date(raw_date):
    for d in date_format:
    try:
    dt = arrow.get(raw_date,d).date()
    # if you want str date with custom format
    # dt = arrow.get(raw_date,d).format('YYYY-MM-DD')
    print(f'time is [{dt}]')
    break
    except:
    continue

    for t in tt:
    format_date(t)
    ```
    wlwood
        7
    wlwood  
       Aug 15, 2018
    我去,果然是时间问题。
    misaka19000
        8
    misaka19000  
       Aug 15, 2018
    @aijam #2 难道不是 3 月就有 37 日😂
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   909 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 36ms · UTC 21:59 · PVG 05:59 · LAX 14:59 · JFK 17:59
    ♥ Do have faith in what you're doing.