[Tutor] padding frames
Danny Yoo
dyoo at hkn.eecs.berkeley.edu
Sun Jan 11 02:00:09 EST 2004
On Sat, 10 Jan 2004, Christopher Spears wrote:
> I'm trying to figure out way to use Python to pad frames. The name of
> the frames follow the this format DSCN-2.JPG. What would be the best
> strategy to use Python to convert DSCN-2.JPG to DSCN-0002.JPG?
Hi Christopher,
There's a padding method in strings called 'zfill':
###
>>> '42'.zfill(4)
'0042'
###
So using zfill() on the numeric portion of each of your filenames should
do the trick. Doing zfill() directly on filenames like:
###
>>> 'dsnc-2.jpg'.zfill(4)
'dsnc-2.jpg'
###
won't work. But if we can somehow extract the numeric portion, then we
should be in business:
###
>>> 'dsnc-' + '2'.zfill(4) + '.jpg'
'dsnc-0002.jpg'
###
If you have more questions on this, please feel free to ask. Good luck!
More information about the Tutor
mailing list