You are not logged in.

#1 2006-12-08 02:21:24

skottish
Forum Fellow
From: Here
Registered: 2006-06-16
Posts: 7,942

Simple Python question

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

#2 2006-12-08 03:28:18

skottish
Forum Fellow
From: Here
Registered: 2006-06-16
Posts: 7,942

Re: Simple Python question

Hmmm... the post I was responding to vanished. Now it's back...

Offline

#3 2006-12-08 03:28:45

T-Dawg
Forum Fellow
From: Charlotte, NC
Registered: 2005-01-29
Posts: 2,736

Re: Simple Python question

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

#4 2006-12-08 03:29:57

skottish
Forum Fellow
From: Here
Registered: 2006-06-16
Posts: 7,942

Re: Simple Python question

Thanks a bunch.

I have to say that Python is a such a joy to work with.

Offline

#5 2006-12-08 03:30:08

T-Dawg
Forum Fellow
From: Charlotte, NC
Registered: 2005-01-29
Posts: 2,736

Re: Simple Python question

woops. I deleted it and changed it. This one should work.

Offline

#6 2006-12-08 04:22:49

cactus
Taco Eater
From: t͈̫̹ͨa͖͕͎̱͈ͨ͆ć̥̖̝o̫̫̼s͈̭̱̞͍̃!̰
Registered: 2004-05-25
Posts: 4,622
Website

Re: Simple Python question

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

#7 2006-12-08 05:26:46

neotuli
Lazy Developer
From: London, UK
Registered: 2004-07-06
Posts: 1,204
Website

Re: Simple Python question

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

#8 2006-12-08 06:05:18

stonecrest
Member
From: Boulder
Registered: 2005-01-22
Posts: 1,190

Re: Simple Python question

neotuli++


I am a gated community.

Offline

#9 2006-12-08 09:27:31

sweiss
Member
Registered: 2004-02-16
Posts: 635

Re: Simple Python question

stonecrest wrote:

neotuli++

Hmmm...

>>> neotuli++
  File "<stdin>", line 1
    neotuli++
            ^
SyntaxError: invalid syntax

Offline

#10 2006-12-08 10:24:12

arooaroo
Member
From: London, UK
Registered: 2005-01-13
Posts: 1,268
Website

Re: Simple Python question

I always use zfill for this too.

Offline

#11 2006-12-08 15:54:14

phrakture
Arch Overlord
From: behind you
Registered: 2003-10-29
Posts: 7,879
Website

Re: Simple Python question

I prefer cacti's solution, as it's more common across languages (the same thing will work in C and php).

Offline

#12 2006-12-08 16:07:40

Dusty
Schwag Merchant
From: Medicine Hat, Alberta, Canada
Registered: 2004-01-18
Posts: 5,986
Website

Re: Simple Python question

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?

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

#13 2006-12-08 22:08:53

skottish
Forum Fellow
From: Here
Registered: 2006-06-16
Posts: 7,942

Re: Simple Python question

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

#14 2006-12-08 22:53:34

phrakture
Arch Overlord
From: behind you
Registered: 2003-10-29
Posts: 7,879
Website

Re: Simple Python question

Dusty wrote:
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

#15 2006-12-08 23:15:00

arooaroo
Member
From: London, UK
Registered: 2005-01-13
Posts: 1,268
Website

Re: Simple Python question

skottish wrote:

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

#16 2006-12-09 00:04:47

pauldonnelly
Member
Registered: 2006-06-19
Posts: 776

Re: Simple Python question

skottish wrote:

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. lol I just know Lisp well enough that it wasn't any trouble to Google'n'guess.

Offline

#17 2006-12-09 01:33:26

yankees26
Member
From: Connecticut, USA
Registered: 2006-09-29
Posts: 190

Re: Simple Python question

I know some Scheme, I just never use it tongue (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. tongue

Offline

#18 2006-12-09 02:14:12

skottish
Forum Fellow
From: Here
Registered: 2006-06-16
Posts: 7,942

Re: Simple Python question

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

#19 2006-12-09 21:13:27

codemac
Member
From: Cliche Tech Place
Registered: 2005-05-13
Posts: 794
Website

Re: Simple Python question

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

Board footer

Powered by FluxBB