Newbie question - leading zeros

E

eldorado

I have looked around and cannot seem to find a way to strip leading zeros
off of values in a dictionary. Basically, I am looking to do a for loop
and any value that has one or more leading zeros would be stripped. Any
pointers would be appreciated. Thanks
 
R

Rainy

eldorado said:
I have looked around and cannot seem to find a way to strip leading zeros
off of values in a dictionary. Basically, I am looking to do a for loop
and any value that has one or more leading zeros would be stripped. Any
pointers would be appreciated. Thanks

import string'1'

Hope this willhelp

-Rainy
 
M

Mitko Haralanov

I have looked around and cannot seem to find a way to strip leading zeros
off of values in a dictionary. Basically, I am looking to do a for loop
and any value that has one or more leading zeros would be stripped. Any
pointers would be appreciated. Thanks

I assume that the values are string representations of numbers? If that
is the case, you could do 'str (int (<value>))'. It's ugly but it works.
 
G

Gary Herron

eldorado said:
I have looked around and cannot seem to find a way to strip leading zeros
off of values in a dictionary. Basically, I am looking to do a for loop
and any value that has one or more leading zeros would be stripped. Any
pointers would be appreciated. Thanks
There is nowhere enough information in your question to answer it well,
but I'll make some guesses.

If your values are strings that have leading zeros, then lstrip may be
what you want.'xyz'

If your values are integers -- then the question does not make sense.
The internal representation of a number (binary zeros and ones) is not
something you can change.

If your values are integers and something is printing them out with
leading zeros -- then change the code that is doing the formatting for
that print. For example, here are several ways to convert an int to a
printable string with and without leading zeros:'00123'

Hope that helps,
Gary Herron
 
T

Timothy Grant

I have looked around and cannot seem to find a way to strip leading zeros
off of values in a dictionary. Basically, I am looking to do a for loop
and any value that has one or more leading zeros would be stripped. Any
pointers would be appreciated. Thanks

Do you really want to be storing the string representation of the int?

int('0000123') will convert the string representation to the number.
 
W

wittempj

Rainy said:
import string
'1'

Hope this willhelp

-Rainy

string.lstrip is deprecated, so when OPs dictionary has as values
strings it's better to do something like

x = {'a key':'0001', 'another key': '0002'}
for k, v in x.iteritems():
x[k] = v.lstrip('0')
 
D

Dennis Lee Bieber

I have looked around and cannot seem to find a way to strip leading zeros
off of values in a dictionary. Basically, I am looking to do a for loop
and any value that has one or more leading zeros would be stripped. Any
pointers would be appreciated. Thanks

Something I didn't see brought up in the other answers... Are we
talking the "data" or the "key"? {"...values in a dictionary" could be
interpreted to mean the pair of key:data}

If the zeros are part of the key, one would need to ensure that all
access (including the initial dictionary population) converts the key's
value -- ie, either convert to native integer or strip from a string
representation.
--
Wulfraed Dennis Lee Bieber KD6MOG
(e-mail address removed) (e-mail address removed)
HTTP://wlfraed.home.netcom.com/
(Bestiaria Support Staff: (e-mail address removed))
HTTP://www.bestiaria.com/
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top