You are not logged in.
Pages: 1
I have a Python loop running through some processes that has a counter that counts from 1 to some arbitrary number n. So:
1 2 3... 99 100 101... n
I need to be able to convert the format to something like:
000001 000002 000003... 000099 000100 000101... n
I will already be casting the integers to strings. I just need to know how to set up the formatting.
Offline
Hmmm... the post I was responding to vanished. Now it's back...
Offline
max_num_digits = 8
current_num_digits = len(n)
if current_num_digits < man_num_digits:
num_pads = max_num_digits - current_num_digits
padded_num = '0'*num_pads + n
else: padded_num = n
:?:
Offline
Thanks a bunch.
I have to say that Python is a such a joy to work with.
Offline
woops. I deleted it and changed it. This one should work.
Offline
holy crap. that is an ugly solution.
try this:
for x in numbers:
print '%08d' % x
the % sting substitution is sprintf syntax.
You can use the above concept more generally of course.
The above outputs 8 chars zero padded, for each digit in the numbers list.
"Be conservative in what you send; be liberal in what you accept." -- Postel's Law
"tacos" -- Cactus' Law
"t̥͍͎̪̪͗a̴̻̩͈͚ͨc̠o̩̙͈ͫͅs͙͎̙͊ ͔͇̫̜t͎̳̀a̜̞̗ͩc̗͍͚o̲̯̿s̖̣̤̙͌ ̖̜̈ț̰̫͓ạ̪͖̳c̲͎͕̰̯̃̈o͉ͅs̪ͪ ̜̻̖̜͕" -- -̖͚̫̙̓-̺̠͇ͤ̃ ̜̪̜ͯZ͔̗̭̞ͪA̝͈̙͖̩L͉̠̺͓G̙̞̦͖O̳̗͍
Offline
or you could do it using python string's zfill() method. See http://docs.python.org/lib/string-methods.html#l2h-270
for your example (to garauntee 6 digits by padding like that):
foo = "5"
foo.zfill(6) ... will return "000005"
The suggestion box only accepts patches.
Offline
neotuli++
I am a gated community.
Offline
neotuli++
Hmmm...
>>> neotuli++
File "<stdin>", line 1
neotuli++
^
SyntaxError: invalid syntax
Some PKGBUILDs: http://members.lycos.co.uk/sweiss3
Offline
I always use zfill for this too.
Offline
I prefer cacti's solution, as it's more common across languages (the same thing will work in C and php).
Offline
I prefer cacti's solution, as it's more common across languages (the same thing will work in C and php).
I'm curious; is that because you think the code is more portable, or because you think its more readable?
Personally, I prefer the stonecrest solution, although I'd consider it more readable if the function was called "zerofill" instead of "zfill". Its shorter.
I suppose its because I never did think printf syntax was elegant in any language though.
Dusty
Offline
This is great! I ask for help with TinyScheme, and I only get one (very much appreciated [thanks pauldonnelly]) person responding to my question. I get smart, dump Script-Fu for Python, and I get a code competition complete with commentary and opinions.
Offline
phrakture wrote:I prefer cacti's solution, as it's more common across languages (the same thing will work in C and php).
I'm curious; is that because you think the code is more portable, or because you think its more readable?
It's because the knowledge, or the impression of it is more easily transferred. If you showed a "C guy" that syntax, he'll get it, but would have to look up "zfill" to know what was going on. Additionally, using this syntax makes printf("Here is %5.3f", foo); easier to grok for non-C coders.
Offline
This is great! I ask for help with TinyScheme, and I only get one (very much appreciated [thanks pauldonnelly]) person responding to my question. I get smart, dump Script-Fu for Python, and I get a code competition complete with commentary and opinions.
I think Python is much more popular and it's such a lucid language (as programming languages go). I've only heard of Scheme being used in academic institutions for teaching purposes.
I've noticed that there are plenty of Python coders on these forums so your queries are likely to be answered.
Offline
This is great! I ask for help with TinyScheme, and I only get one (very much appreciated [thanks pauldonnelly]) person responding to my question. I get smart, dump Script-Fu for Python, and I get a code competition complete with commentary and opinions.
Well, nobody knows Scheme. Heck, I don't even know Scheme. I just know Lisp well enough that it wasn't any trouble to Google'n'guess.
Offline
I know some Scheme, I just never use it (so I have probably forgotten stuff). I'd agree with arooaroo that they are plenty of Python programmers here...and Perl....and C and C++....and hopefully not Java.
Offline
Ain't it purty?
out_file = file_prefix + str(counter).zfill(z_size) + file_suffix
pdb.gimp_file_save(image, drawable, out_file, out_file)
Offline
Scheme is great for learning about functional programming, but common lisp is really what you'd want to learn for industrial use.
PS: I just read On Lisp.
Offline
Pages: 1