I have files generated, where the filenames contains within it an integer, but also has a prefix, like so:
snapshot_data_vss_iter_10000.caffemodel snapshot_data_vss_iter_1000.caffemodel snapshot_data_vss_iter_500.caffemodel
How do I pad the integers with leading zeros so I can use sort()
later?
edit: I am aware of zfill()
, but that requires me to split the string first to extract the integer part of it, pad it and then replace. I was wondering if there's a more pythonic way to do it
Thank you
I would suggest using formatting (which is nice with a format string):
>>> iters = 500
>>> f'snapshot_data_vss_iter_{iters:05}.caffemodel'
'snapshot_data_vss_iter_00500.caffemodel'
The :05
means to left pad to at least 5 characters using zeros. There are various other things you can do with string formatting, I'd suggest reading up .
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.