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

tuple 格式的时间如何转化成'H:M:S'

  •  
  •   songdg · 2020-08-22 10:31:57 +08:00 · 1619 次点击
    这是一个创建于 1314 天前的主题,其中的信息可能已经有所发展或是发生改变。
    例如(9, 34, 16)转成 09:34:16
    8 条回复    2020-08-23 09:56:31 +08:00
    musi
        1
    musi  
       2020-08-22 10:37:58 +08:00   ❤️ 1
    a = (9, 34, 16)
    formated_time = ':'.join(list(map(lambda x: '0'+str(x) if x < 10 else str(x), a)))
    xyfan
        2
    xyfan  
       2020-08-22 10:39:23 +08:00
    没说什么语言没说转成什么结果,问的太潦草了,如果是 Python 中 tuple 转成字符串
    >>>t=(9,34,16)
    >>>print("{0[0]}:{0[1]}:{0[2]}".format(t))
    9:34:16
    xyfan
        3
    xyfan  
       2020-08-22 10:42:31 +08:00   ❤️ 1
    @xyfan 不好意思,没注意到还有一个 0 在前面,改成这样
    >>>print("{:0>2d}:{:0>2d}:{:0>2d}".format(*t))
    09:34:16
    dick20cm
        4
    dick20cm  
       2020-08-22 10:53:40 +08:00
    这种伸手党问题何必回复,@musi @xyfan 你俩就是闲的。。
    ysc3839
        5
    ysc3839  
       2020-08-22 10:56:35 +08:00 via Android   ❤️ 1
    import datetime

    t = (9, 34, 16)
    tm = datetime.time(*t)
    print(tm.strftime('%H:%M:%S'))
    musi
        6
    musi  
       2020-08-22 11:41:15 +08:00
    @dick20cm 是挺闲的,会点基础总有办法写出来,不会基础百度也能百度出来😂
    songdg
        7
    songdg  
    OP
       2020-08-23 09:55:31 +08:00
    @ysc3839 谢谢,帮助
    songdg
        8
    songdg  
    OP
       2020-08-23 09:56:31 +08:00
    @dick20cm 因为急用,一时又找不到所以才来这里求助。
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   1191 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 24ms · UTC 18:25 · PVG 02:25 · LAX 11:25 · JFK 14:25
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.