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

一个简单的 python3 datetime 问题

  •  
  •   Huelse · May 6, 2019 · 3369 views
    This topic created in 2587 days ago, the information mentioned may be changed or developed.
    import datetime
    time_tuple = (2019, 5, 6, 13, 8, 10)
    print(datetime.datetime(time_tuple))
    

    报错:

    TypeError: an integer is required (got type tuple)
    

    但是 print(datetime.datetime(2019, 5, 6, 13, 8, 10))完全没问题

    11 replies    2019-05-07 11:39:57 +08:00
    mxy940127
        1
    mxy940127  
       May 6, 2019
    datetime.datetime(year, month, day[, hour[, minute[, second[, microsecond[, tzinfo]]]]])
    看 api 文档
    nondanee
        2
    nondanee  
       May 6, 2019 via Android
    print(datetime.datetime(*time_tuple))
    Osk
        3
    Osk  
       May 6, 2019
    print(datetime.datetime(time_tuple)) --> print(datetime.datetime(*time_tuple))
    Gakho
        5
    Gakho  
       May 6, 2019
    #2 #3 正解,你应该看一下不定参数的函数是怎么调用的
    KevinBu
        6
    KevinBu  
       May 6, 2019
    datetime.datetime() 第一个参数是 year

    你给它一个元组,怎么能不报错
    gazhang
        7
    gazhang  
       May 6, 2019
    @KevinBu 正解
    pkookp8
        8
    pkookp8  
       May 6, 2019 via Android
    报错的给了一个参数,是 tuple
    不报错的给了 6 个参数
    Huelse
        9
    Huelse  
    OP
       May 6, 2019
    @mxy940127 #1
    @nondanee #2
    @Osk #3
    @Osk #4
    @Gakho #5
    @KevinBu #6
    @gazhang #7
    @pkookp8 #8
    感谢各位,其实一开始看了文档,当时处于蒙蔽状态,没弄懂,才过来问下,谢谢了!
    cominghome
        10
    cominghome  
       May 7, 2019
    这种很明显的报错完全可以自己解决的啊兄弟
    neoblackcap
        11
    neoblackcap  
       May 7, 2019
    import datetime
    time_tuple = (2019, 5, 6, 13, 8, 10)
    print(datetime.datetime(*time_tuple))
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   1081 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 879ms · UTC 23:29 · PVG 07:29 · LAX 16:29 · JFK 19:29
    ♥ Do have faith in what you're doing.