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
frmongo
V2EX  ›  Python

时间问题

  •  
  •   frmongo · 2018-08-15 10:24:06 +08:00 · 1669 次点击
    这是一个创建于 2074 天前的主题,其中的信息可能已经有所发展或是发生改变。

    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 条回复    2018-08-15 14:11:42 +08:00
    frmongo
        1
    frmongo  
    OP
       2018-08-15 10:30:44 +08:00
    说明:t1 t2 t3 里没有空格,没有空格,这个是 V2EX 显示问题
    aijam
        2
    aijam  
       2018-08-15 10:32:05 +08:00
    3 月有 37 日?
    frmongo
        3
    frmongo  
    OP
       2018-08-15 10:37:22 +08:00
    哈哈,我就是试试,忘了这茬
    frmongo
        4
    frmongo  
    OP
       2018-08-15 10:39:06 +08:00
    我去,我真是晕了
    hahastudio
        5
    hahastudio  
       2018-08-15 10:41:13 +08:00
    话说回来,如果你真的确定你发帖的时候没有空格,可以 @ 一下 Livid,是不是 V2EX 自动给数字和中文之间加的空格,对 code block 也起作用了
    princelai
        6
    princelai  
       2018-08-15 14:05:58 +08:00
    如果你能保证都是中文的年月这种格式,我倒是有一个好办法

    ```
    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  
       2018-08-15 14:09:00 +08:00
    我去,果然是时间问题。
    misaka19000
        8
    misaka19000  
       2018-08-15 14:11:42 +08:00
    @aijam #2 难道不是 3 月就有 37 日😂
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   2845 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 29ms · UTC 13:54 · PVG 21:54 · LAX 06:54 · JFK 09:54
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.