Home > Mobile >  Change commas to dots in Excel with Python
Change commas to dots in Excel with Python

Time:06-22

I print the tables I scanned from the internet with Selenium to Excel. However, this table contains values such as "15,300". I would like to print these values as "15,300" or "15.3" and change the cells that are normally "1.022" to "1022". And I add schedule for every 5 mins.

In short, I want to remove the dot from the dotted values, and convert the comma to a dot.

I take the whole table as a class and then split it into parts. Here is my code: (Unfortunately, since it is company data, I can only share a certain part.)

driver.get(urlTarget)
    a=driver.find_element_by_class_name("dataTable").text
    #print(a)
    x = a.split('\n')
    x[0] = "Mak.No. AEF% SEF% DAS% YLW% YF/Y MIS% RTI% NDOF PRKG(kg) DOF% NCOP YEF% YEF-A% YEF-B% FBE% FBE-A% FBE-B% SBE% SBE-A% SBE-B% NCTB NT/T"
    for i in range (len(x)-1):
        if i != 0:
            x[i]=x[i 1]
    #print(x)
    workbook = xlsxwriter.Workbook('C:/Users/...xxx.xlsx')
    worksheet = workbook.add_worksheet()

In the code above, I deleted the column names and the places I wanted to delete. The place where the actual process will pass is in the code below.

    row = 0
    column = 0
    b=x[:(len(x)-1)]    
    for i in b:
        c=i.split()
        column=0
        for item in c :
            worksheet.write(row, column, item)
            column =1
        row  = 1
    workbook.close()

This writes the table to Excel. But I need changes. When I use item=item.str.replace(',','.') in the for loop, it will returns error that 'str' object has no attribute 'str'

CodePudding user response:

first convert the number to string and if it is already string then don't use ".str"

item = item.replace(',','.')

or

item = str(item).replace(',','.')

CodePudding user response:

I believe you're on the wrong track here: the dots and commas you're dealing with are decimal and thousand separators. In order to work with them, you might alter the format of your cells, as shown in this dialog box for one particular cell:

Excel cell formatting

There also are the regional settings of your Windows computer:

Windows regional settings, number formatting

  • Related