Home > Back-end >  How to change a parsed text into integer or remove decimal points?
How to change a parsed text into integer or remove decimal points?

Time:02-02

How to convert a text into integer? The "reference_price" parsed from a webpage is "123.45" and its type is Text. However, I would like to change this to an integer like "123".

###### Parsing   
tbody = table.tbody
    for i, tr in enumerate(tbody.find_all('tr')):      
        reference_price = tr.find('td').text
    
###### Convert to datafrme 
reference_price_list = reference_price_list.append(reference_price)
    df = pd.DataFrame(data=zip(reference_price_list), columns=['price'])

I have tried both int("reference_price") and reference_price.astype(int) but doesn't seem to solve the problem.

CodePudding user response:

IIUC, before you can cast reference_price to an (int), you need to make sure to retain only the whole number (the left part of the decimal-like). One way, is to use str.split with slicing :

reference_price_list = []

for tr in table.tbody.find_all("tr"):
    reference_price = (tr.find("td").text).split(".")[0]
    reference_prise = int(reference_prise.replace(",",""))
    reference_price_list.append(reference_price)

df = pd.DataFrame({"price": reference_price_list}) # <- df.dtypes|price:int64

NB : You may need to include a try..except statement to catch an eventual ValueError.

CodePudding user response:

Converting the text into float then converting into int would work

Example:

reference_price = int(float(tr.find('td').text))
  • Related