Convert String to Integer, Preserving Leading Zeroes - python

I wrote a function that reads from a file and checks each line according to some conditions. Each line in the file contains a number. In the function itself, I'd like to add to this number and check it again so I tried the following:
str(int(l.strip())+1)) # 'l' being a line in the file
I noticed that I got some faulty results each time I cast a number with leading zeroes. (The file contains every possible 6-digit number, for example: 000012).
I think that the conversion to integer just discards the unnecessary leading zeroes, which throws off my algorithm later since the string length has changed.
Can I convert a string to an integer without losing the leading zeores?

If you want to keep the zero padding on the left you should convert the number to a string again after the addition. Here's an example of string formatting for zero padding on the left for up to six characters.
In [13]: "%06d" % 88
Out[13]: '000088'

Related

How to grab hex value in between a bunch of zeros and convert it to decimal?

I wanted to try and grab a hex value in between a bunch of zeros and convert it to decimal. Here's a sample: '00000000002E3706400000'. So I only want to grab '2E37064' and disregard everything else around it. I know to use the int() function to convert it to decimal, but when I do, it includes the leading zeros right after the actual hex value. Here's a sample of my code:
hex_val = '00000000002E3706400000'
dec_val = int(hex_val, 16)
print(dec_val)
And then here's the output:
50813862936576
The actual value I want is:
48459876
Is there an optimal way to accomplish this?
You can use the .strip() function to remove the leading and trailing zeroes (though removing the leading zeroes here isn't technically necessary):
int(hex_val.strip('0'), 16)
This outputs:
48459876

Keep zero digit save while converting string to integer in python

I am converting a string into integer using int function and it is working fine but i want to keep save zero digit that are at the start of the string.
string_value = '0123'
print(int(string_value))
result is 123
How can i format output 0123 as in integer type value not in string.
You can't, but if you want to put 0's (zero padding) at the beginning of your number, this is the way to do it.
"{:04}".format(123)
# '0123'
"{:05}".format(123)
# '00123'
Like every one said you can try above answers or the following :
string_value = '0123'
int_no = int(string_value)
print("%04d" % int_no)
print(string_value.zfill(4))
Both will give same answer
Impossible, you cannot get an integer value of 0123.
You should change your mind, you do not actually need 0123 in integer, but you need to keep zero when displaying it. So the question should change to how to format output.

what's the different between "%0.6X" and "%06X" in python?

what's the different between "%0.6X" and "%06X" in python ?
def formatTest():
print "%0.6X" %1024
print "%06X" %1024
if __name__=='__main__':
formatTest()
The result is :
000400
000400
https://docs.python.org/2/library/stdtypes.html#string-formatting
A conversion specifier contains two or more characters and has the following components, which must occur in this order:
The '%' character, which marks the start of the specifier.
Mapping key (optional), consisting of a parenthesised sequence of characters (for example, (somename)).
Conversion flags (optional), which affect the result of some conversion types.
Minimum field width (optional). If specified as an '*' (asterisk), the actual width is read from the next element of the tuple in values, and the object to convert comes after the minimum field width and optional precision.
Precision (optional), given as a '.' (dot) followed by the precision. If specified as '*' (an asterisk), the actual width is read from the next element of the tuple in values, and the value to convert comes after the precision.
Length modifier (optional).
Conversion type.
So the documentation doesn't clearly state what the interaction of width versus precision is. Let's explore some more.
>>> '%.4X' % 1024
'0400'
>>> '%6.4X' % 1024
' 0400'
>>> '%#.4X' % 1024
'0x0400'
>>> '%#8.4X' % 1024
' 0x0400'
>>> '%#08.4X' % 1024
'0x000400'
Curious. It appears that width (the part before .) controls the whole field, and space-pads by default, unless flagged with 0. Precision (the part after .) controls only the integer part, and always 0-pads.
Let's take a look at new-style formatting. It's the future! (And by future I mean it's available now and has been for many years.)
https://docs.python.org/2/library/string.html#format-specification-mini-language
width is a decimal integer defining the minimum field width. If not specified, then the field width will be determined by the content.
When no explicit alignment is given, preceding the width field by a zero ('0') character enables sign-aware zero-padding for numeric types. This is equivalent to a fill character of '0' with an alignment type of '='.
The precision is a decimal number indicating how many digits should be displayed after the decimal point for a floating point value formatted with 'f' and 'F', or before and after the decimal point for a floating point value formatted with 'g' or 'G'. For non-number types the field indicates the maximum field size - in other words, how many characters will be used from the field content. The precision is not allowed for integer values.
Much more clearly specified! {0:06X} is valid, {0:0.6X} is not.
>>> '{0:06x}'.format(1024)
'000400'
>>> '{0:0.6x}'.format(1024)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: Precision not allowed in integer format specifier
06 means that the data passed in, if less than 6 digits long will be prepended by 0 to fill that space. The x denotes the type of data, in this case the format string is expecting a hexadecimal number.
1024 in hexadecimal is 400, which is why you get 000400 as your result.
For 0.6x, the . denotes the precision (width) of characters to be shown. So, %0.6x means:
% - start of the format string specification
A 0, which means that for numerical values, pad them by 0 to meet the format specification.
A . which precision modifier. The number after this (6) is how much width to give for the precision characters.
Finally, the x which is the conversion type, in this case hexadecimal.
Since hexadecimal numbers don't have float components, the results of both those operations are the same.
These happen to be equivalent. That doesn't mean you can always ignore the ., though; the reason for the equivalence is pretty specific and doesn't generalize.
0.6X specifies a precision of 6, whereas 06X specifies a minimum field width of 6. The documentation doesn't say what a precision does for the X conversion type, but Python follows the behavior of printf here, where a precision for X is treated as a minimum number of digits to print.
With 06X, the formatting produces at least 6 digits, adding leading zeros if necessary. With 0.6X, the formatting produces a result at least 6 characters wide. The result would be padded with spaces, but the 0 says to pad with zeros instead. Overall, the behavior works out to be the same.

How to convert octal to string without change the value?

How to convert 00024 to '00024' with python?
If I run 00024 in python shell, it print 20 as result. I want to convert 00024 to string, with the result is '00024'. Any suggestions?
If you want to represent integers with leading zeros, format() the number to a string explicitly:
format(integer, '05d')
Whenever you output values, print implicitly converts it to a string with str(), with format() you get to control how that conversion takes place explicitly.
When you echo a number in the interactive interpreter, repr() is used instead, but for integers the output is exactly the same as when you use str().
Demo:
>>> format(24, '05d')
'00024'
When you enter 00024 as an integer literal, Python 2 parses that as an octal number, in Python 3 that's a syntax error.
If you want to interpret such a number in the Python 2 shell as if it was a string, you cannot do what you want, not with an arbitrary number of leading zeros. You can, at best, re-format the resulting integer as octal, again with leading zeros. That'll produce a string again, but you have to hardcode the number of leading zeros:
>>> number = 00024
>>> format(number, '05o')
'00024'
Note that this'll also fail as soon as you have a number that doesn't have a leading zero; you could auto-detect such values if they are greater than 4095 (07777 octal):
strnumber = format(number, '5d' if number > 0o07777 else '05o')
and any number with leading zero and the digits 8 or 9 simply fails with a syntax error:
>>> 09876
File "<stdin>", line 1
09876
^
SyntaxError: invalid token
Your better bet is to just enter such numbers as strings instead.

removing a % sign and parsing result as an integer

I am scraping an XML file and returning a load of percentages, pulled out directly as a percentage sometimes negative with the % sign already attached e.g.
-38%
-2%
4%
25%
I am trying to do a filter such as this:
if percentage < 20.0 : continue;
However I cannot perform this filter, I assume as a result of the % symbol.
For reference I use:
cell['roi']
To get the percentages, iterating through each row using:
for row in xmlload1['rows']:
cell = row["cell"]
How do I get around this % symbol? Is there an easy way?
You can't perform that filter because you're trying to compare a string (like "4%") to a float (20.0). In Python 3, this will raise a TypeError; in Python 2, it will "work", but all strings will be treated as greater than the number 20.0, so it won't do any good.
You need to convert the string to a float before you can use it as a float. But you don't want to convert the whole string, just the part before the "%" character. (Because 4% isn't a number, it's only the 4 that's a number.)
So, let's do it in two steps: use rstrip to remove the "%", then use float to convert it to a float.
cell = float(row["cell"].rstrip("%"))
You can pass a string to strip which will strip the characters passed in the passed string, the below will strip %, newlines and spaces:
cell = int(row["cell"].strip("%\n "))

Categories

Resources