Python Forum
openpyxl convert data to float
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
openpyxl convert data to float
#1
i have problem-related with excel, it seems like convert from txt to excel the data is a string not float. Is there any solution to convert it to float?

I have a text file name result.txt as below
datettime              Tput 
20221018.165317.401555 0.000545
20221018.165327.401723 0.001422
20221018.165337.401893 0.001283
20221018.165347.402060 0.000476
excel.py will convert the txt file to excel
import openpyxl, string
from openpyxl.utils import get_column_letter
from openpyxl.styles import Font

def excelconvertMAC(result):
    #f = open(r'C:\MAC_add_Submission\data_out.txt', 'r+')  # open text
    f = open(result, 'r+')  # open text
    #########if load excel file ########################
    # excel=openpyxl.load_workbook(r'D:\\test\\test.xlsx') #open excel
    # excel=openpyxl.load_workbook(r'D:\\test\\test.xlsx') #open excel
    excel = openpyxl.Workbook()
    sheet = excel.worksheets
    line = f.readline();  # read text

    while line:
        list123 = []
        #list123 = line.split(sep=' ')  # convert,
        list123 = line.split()  # convert,
        #print(type(list123[1]))
        #print(list123)
        for i in range(0, len(list123)):  # remove space
            list123[i] = list123[i].strip('\n')

        sheet[0].append(list123)  # write into excel
        
        #print(sheet[0])
        # sheet[0].column_dimensions.width = 20

#adjust the column width 
        column = 1
        while column < 6:
            i = get_column_letter(column)  
            #print(i)         
            sheet[0].column_dimensions[i].width = 25            
            column += 1
        sheet[0]['A1'].font = Font(size = 14, bold = True)
        sheet[0]['B1'].font = Font(size = 14, bold = True
        line = f.readline()  # read next line
    excel.save('result.xlsx')
#resultfilename=input("please enter your report txt file name: ")
resultfilename="result.txt"
excelconvertMAC(resultfilename)
After converting to excel, open excel you will see that the second columns seem like strings not float.
Does anyone know how to change it?
please refer to the excel picture, the column has a green mark which is an error, I want to show it as float not string

Attached Files

Thumbnail(s)
   
Reply


Messages In This Thread
openpyxl convert data to float - by jacklee26 - Nov-08-2022, 05:20 AM
RE: openpyxl convert data to float - by rob101 - Nov-08-2022, 09:54 AM
RE: openpyxl convert data to float - by jacklee26 - Nov-09-2022, 12:25 AM
RE: openpyxl convert data to float - by rob101 - Nov-09-2022, 12:37 AM
RE: openpyxl convert data to float - by jacklee26 - Nov-09-2022, 02:07 AM
RE: openpyxl convert data to float - by rob101 - Nov-09-2022, 03:24 AM
RE: openpyxl convert data to float - by jacklee26 - Nov-09-2022, 07:36 AM
RE: openpyxl convert data to float - by deanhystad - Nov-09-2022, 03:25 PM
RE: openpyxl convert data to float - by jacklee26 - Nov-19-2022, 08:23 AM
RE: openpyxl convert data to float - by deanhystad - Nov-09-2022, 05:58 AM
RE: openpyxl convert data to float - by jacklee26 - Nov-09-2022, 07:29 AM
RE: openpyxl convert data to float - by rob101 - Nov-09-2022, 12:55 PM
RE: openpyxl convert data to float - by deanhystad - Nov-09-2022, 03:13 PM
RE: openpyxl convert data to float - by deanhystad - Nov-19-2022, 11:59 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  python calculate float plus float is incorrect? sirocawa 6 419 Apr-16-2024, 01:45 PM
Last Post: DeaD_EyE
  Convert File to Data URL michaelnicol 3 1,273 Jul-08-2023, 11:35 AM
Last Post: DeaD_EyE
  convert string to float in list jacklee26 6 2,009 Feb-13-2023, 01:14 AM
Last Post: jacklee26
  Convert SQLite Fetchone() Result to float for Math Extra 13 3,716 Aug-02-2022, 01:12 PM
Last Post: deanhystad
  Convert string to float problem vasik006 8 3,504 Jun-03-2022, 06:41 PM
Last Post: deanhystad
  Convert nested sample json api data into csv in python shantanu97 3 2,940 May-21-2022, 01:30 PM
Last Post: deanhystad
  How to convert binary data into text? ZYSIA 3 2,689 Jul-16-2021, 04:18 PM
Last Post: deanhystad
  Yahoo_fin, Pandas: how to convert data table structure in csv file detlefschmitt 14 7,926 Feb-15-2021, 12:58 PM
Last Post: detlefschmitt
  Binary data to Image convert Nuwan16 1 5,766 Aug-24-2020, 06:03 AM
Last Post: millpond
  Formatting Data/Time with Pyodbc and openpyxl bearcats6001 0 2,315 Aug-17-2020, 03:44 PM
Last Post: bearcats6001

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020