This question already has answers here:
Formatting floats without trailing zeros
(21 answers)
Closed 6 years ago.
I want to extract telephone number from *.xls with module xlrd using Python, but it shows like this:'16753435903.0'.
Here's the code:
opensheet = openxls.sheet_by_name(u'sms')
sheetrows = opensheet.nrows
for rownum in range(1, sheetrows):
rowvalue = opensheet.row(rownum)
execinfo = ""
for colnum in range(1,5):
execinfo += "'"+str(rowvalue[colnum].value)+"',"
The key part:
str(rowvalue[colnum].value)
How can I get the well format telephone number without .0?
This might be a very simple workaround: convert the number to an int before converting it to a string?
Related
This question already has answers here:
Python int to binary string?
(36 answers)
Closed last year.
How can i loop through a decimal number and print out a binary figure for that decimal number.
This is the code i tried using python
dec_Num = 1200
for i in dec_Num;
print i
Because the goal is to print we can generate a string
def DecimalToBinary(num):
strin=""
while num >= 1:
strin+=str(num%2)
num=num // 2
return strin[::-1]
This question already has answers here:
How can I convert a string with dot and comma into a float in Python
(9 answers)
Closed 2 years ago.
I have a problem with turning a string into a float value. I'm screaming a website and trying to get the prices in to float values, but the problem is that the prices can look like this:
$2,549.98
$2,262.64
$999.00
marketprice = driver.find_element_by_xpath('/html/body/app/content-holder/marketplace-detail/landfield-detail/div/div/div[2]/div/div[1]/div[2]/span')
userprice = driver.find_element_by_xpath('/html/body/app/content-holder/marketplace-detail/landfield-detail/div/div/div[2]/div/div[1]/div[6]/span')
print(marketprice.text, userprice.text)
imarketprice = float(marketprice.text[1:])
iuserprice = float(userprice.text[1:])
When I try to convert the error I get:
ValueError: could not convert string to float: '2,549.98'
Is the problem with it that there are a comma and a dot?
Just remove the commas using:
imarketprice = float(marketprice.text[1:].replace(",", ""))
This question already has answers here:
Convert to binary and keep leading zeros
(10 answers)
Closed 2 years ago.
Hi i would like to add leading zeros into binary to make it to have 8 digits. I've tried using zfill() but it doesnt seem to work and i have no idea how to use format() cause all the answers i've found are all keeping the 0b which i dont want that.
Here's what i've tried:
lenToBin = bin(payloadLength).replace("0b", "")
if payloadLength == 30 or payloadLength == 31:
binResult = lenToBin.zfill(3)
else:
binResult = lenToBin.zfill(2)
Is this what you want:
lenToBin = bin(payloadLength).replace("0b", "").zfill(8)
This question already has answers here:
How do I create an incrementing filename in Python?
(13 answers)
convert number to formatted string in python with prefix 0s
(3 answers)
Closed 2 years ago.
proId = 'nam001'
I want to increment numeric values only. That should be like this 'nam002', 'nam003'.
How to do this?
Just because you asked nicely :D
You can use the simplest for loop, with str.zfill to pad with zeroes, then add it to the 'nam' prefix like this:
for i in range(1,11):
proId = 'nam' + str(i).zfill(3)
print(proId) # or do whatever you need with it...
Output:
nam001
nam002
...
nam009
nam010
This question already has answers here:
How to extract numbers from a string in Python?
(19 answers)
extract digits in a simple way from a python string [duplicate]
(5 answers)
Closed 3 years ago.
I have the following string:
string_a = 81^A55
from which I'm trying to get the following list
string_a_int = [81,55]
I'm able to split the string into a list made by numbers as follows
list_only_number = re.split('[A-Z]+', string_a)
list_only_numbers = [81^, 55]
but I'm trying to figure out how to skip also the ^.
Any help would be much appreciated.