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 "))
Related
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
I have a string of ones and zeros, which typically has a length of 8*n, since they come from "n" bytes.
Now I want to arrange them into groups of five and I want to fill up the string with "0" until there are 16 5-bit "bytes" in total.
This is what I came up with but I can't figure out why this is not working.
while(len(binary_i) // (5*16) != 0):
binary_i = binary_i + "0"
Your problem seems to be that you're using // instead of %. Also, if you want to use a Python built-in alternative instead of using this while loop, try this:
binary_i.ljust(5*16, '0') # Fill `binary_i` using `0` up to 5*16 characters
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.
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'
I have a string variable:
str1 = '0000120000210000'
I want to convert the string into an integer without losing the first 4 zero characters. In other words, I want the integer variable to also store the first 4 zero digits as part of the integer.
I tried the int() function, but I'm not able to retain the first four digits.
You can use two integers, one to store the width of the number, and the other to store the number itself:
kw = len(s)
k = int(s)
To put the number back together in a string, use format:
print '{:0{width}}'.format(k, width=kw) # prints 0000120000210000
But, in general, you should not store identifiers (such as credit card numbers, student IDs, etc.) as integers, even if they appear to be. Numbers in these contexts should only be used if you need to do arithmetic, and you don't usually do arithmetic with identifiers.
What you want simply cannot be done.. Integer value does not store the leading zero's, because there can be any number of them. So, it can't be said how many to store.
But if you want to print it like that, that can be done by formatting output.
EDIT: -
Added #TimPietzcker's comment from OP to make complete answer: -
You should never store a number as an integer unless you're planning on doing arithmetic with it. In all other cases, they should be stored as strings