python - 需要将时间转换为 h:m:s
问题描述
我的时间格式缺少 36:21 或不完整的小时格式,如 1:23:30,想将其转换为标准时间格式,如 00:00:00,但我不知道为什么我的代码不起作用.
需要像这样转换时间格式 H:M:S --> 00:00:00 len(x) == 6 即 36:21 ; len(x) == 7 即 1:23:30
想得到 00:36:21 和 01:23:30
for x in df7[' Chip Time']:
if len(x) == 6:
x = '00:'+ x
elif len(x) == 7:
x = '0'+ x
print (df7[' Chip Time'])
==================================================== ========= 需要支持。谢谢!
解决方案
你应该使用 apply 方法,它比迭代快得多
我还建议您阅读https://thispointer.com/python-how-to-pad-strings-with-zero-space-or-some-other-character/
这应该工作:
df7[' Chip Time'] = df7[' Chip Time'].apply(lambda x : x.rjust(5, '0').rjust(6, ':').rjust(8, '0'))
推荐阅读
- java - 在 Tomcat 服务器中记录 Slf4j 以获取 cosole 日志
- azure - Azure:创建没有服务 URL 的新 API 管理服务
- javascript - react-scripts babel scripts 错误在本地反应应用程序上使用纱线运行
- visual-studio-code - CMake中是否有可能不扫描特定文件夹?
- c - 使用clang而不是gcc链接成功(Debian Bullseye amd64上的-llzma)
- javascript - 如何使用 node-gyp 构建 python?
- swift - 从 Swift 包中加载字体
- java - SnakeYaml 输出 yaml 与转储 yaml 不匹配
- javascript - 无法理解为什么 JS 代码不在 JS Bin 中运行
- sql - UTL_FILE 保存在本地机器中