python - 在python中格式化文件名
问题描述
我想根据输入格式化字符串。例如,如果行数为 1,则输出必须为image00001.png
,如果行数为 400,则输出为image00400.png
我使用以下方法这样做
filename = "image%05d.png".format(line_count)
但它没有按预期工作。我错过了什么?
解决方案
在 Python 3 中,您可以使用 f 字符串:
filename = f'image{line_count:05}.png'
推荐阅读
- git - 是否有必要从我们的 CI 管道提交 DVC 文件?
- vue.js - Vue CLI 从项目模块动态导入
- c# - 在 C# 中使用 Google OR-Tools 寻找目标?
- sql - while循环完成时变量值丢失
- c# - MS Word - 默认另存为类型是网页
- java - 在 log4j 中使用 RollingFileAppender 时覆盖到相同的日志文件
- java - 如何从@Advice.OnMethodExit 中的 premain 方法中获取 arg?
- python - Knight's Tour in Python - 从前辈那里获得路径
- c - 集合 {a, e, i, o, u, A, E, I, O, U} 中的字母被认为是有声的。对于单词“caracter”将显示在屏幕上:“caparapacteper”
- linear-regression - 如何使用 Orange 执行简单的线性回归