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

非标准时间格式( 2017-9-5 12:00:00)怎么处理成标准时间?

  •  
  •   chen2016 · 2017-09-05 11:49:39 +08:00 · 3419 次点击
    这是一个创建于 2396 天前的主题,其中的信息可能已经有所发展或是发生改变。

    比如采集的时间格式:2017-9-5 12:00:00,对于这种非标准的时间怎么处理成标准时间格式?

    1. 直接 datetime 包处理?好像不行,因为%m 对应的是 01-12,%d 对应 01-31
    2. 对这种时间先拆分再处理拼接?好像可以,不过麻烦点。比如:
    ftime='2017-9-5 12:00:00'
    date,time=ftime.split(' ')
    year,month,day=date.split('-')
    if len(month)==1:
        month='0'+month
    if len(day)==1:
        day='0'+day
    ftime2='-'.join([year,month,day])+' '+time
    

    6286d3d1-610d-48e6-9dc6-3cb85b8ffe05.png

    对于第 2 种方法,我是不满意的。不知道有没有更简便的方法?

    14 条回复    2017-09-05 21:30:34 +08:00
    haddy
        1
    haddy  
       2017-09-05 12:12:35 +08:00   ❤️ 1
    >>> datetime.datetime.strptime("2017-9-5 12:30:45", "%Y-%m-%d %H:%M:%S")
    datetime.datetime(2017, 9, 5, 12, 30, 45)
    haddy
        2
    haddy  
       2017-09-05 12:16:32 +08:00
    python 文档里描述得不是特别清晰,实际上是可以用的
    187j3x1
        3
    187j3x1  
       2017-09-05 12:17:51 +08:00
    chen2016
        4
    chen2016  
    OP
       2017-09-05 12:20:52 +08:00
    @haddy #1 居然可以这样。我应该先试试的。谢谢啦
    chen2016
        5
    chen2016  
    OP
       2017-09-05 12:21:44 +08:00
    @187j3x1 #3 不用自作聪明。我问之前肯定有搜文档
    billgreen1
        6
    billgreen1  
       2017-09-05 13:07:59 +08:00
    有的,使用 pandas 的 Timestamp

    import pandas as pd

    pd.Timestamp("2017-9-5 13:05")

    你会的到你想要的
    qiayue
        7
    qiayue  
       2017-09-05 13:12:00 +08:00
    果然还是 PHP 最好
    date('Y-m-d H:i:s', strtotime('2017-9-5 12:00:00'))
    chen2016
        8
    chen2016  
    OP
       2017-09-05 13:17:22 +08:00
    @billgreen1 #6 一楼已经解决了。datetime 也可以处理
    billgreen1
        9
    billgreen1  
       2017-09-05 15:07:13 +08:00
    @chen2016 一楼的解决方案需要你指定格式,我给出的这个,你不需要指定格式的。哪怕你用 2017/9/5 它也能正确识别
    Martin9
        10
    Martin9  
       2017-09-05 15:11:50 +08:00
    现在 v 站越来越多三楼这种人
    block 了
    chen2016
        11
    chen2016  
    OP
       2017-09-05 15:17:42 +08:00
    @billgreen1 #9 啊!厉害了,谢谢
    Les1ie
        12
    Les1ie  
       2017-09-05 15:53:05 +08:00
    ```
    $ pip install arrow
    ```
    放弃 date 和 time 转到 arrow 吧...
    Les1ie
        13
    Les1ie  
       2017-09-05 15:55:33 +08:00
    突然发现 arrow.get()也拿不到这句话..
    sivacohan
        14
    sivacohan  
       2017-09-05 21:30:34 +08:00 via Android
    这种情况,除了 @haddy 的说明。还有几个库。
    Python-dateutil
    pytz
    arrow
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   1129 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 27ms · UTC 18:45 · PVG 02:45 · LAX 11:45 · JFK 14:45
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.