Python Forum
openpyxl convert data to float
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
openpyxl convert data to float
#11
The easiest way to do the conversion is open the text file in excel and save as a spreadsheet. Using pandas isn't much harder.
import pandas as pd

pd.read_csv("test.txt").to_excel("test.xlsx", index=False)
Your odd text file format with all the extra spaces in the first row messes this up. Some special input processing is required.
import pandas as pd

with open("test.txt", "r") as file:
    column_names = next(file).split()
    df = pd.read_csv(file, sep=" ", names=column_names)
df.to_excel("test.xlsx", index=False)
You also wanted to do some formatting in the excel file. That requires creating an excel writer object and adding styles.
import pandas as pd

with open("test.txt", "r") as file:
    column_names = next(file).split()
    df = pd.read_csv(file, sep=" ", names=column_names)

# Create a writer for doing formatting
writer = pd.ExcelWriter("test.xlsx", engine="xlsxwriter", )
df.to_excel(writer, sheet_name="Data", index=False)
sheet = writer.sheets["Data"]

# Set column widths
sheet.set_column(0, len(column_names)-1, 25)

# Set font for column headers.  May not need because writer already
# uses a special font for the header.
header_format = writer.book.add_format({'bold':True, 'font_size':14})
for index, name in enumerate(column_names):
    sheet.write(0, index, name, header_format)

writer.save()
jacklee26 likes this post
Reply
#12
(Nov-09-2022, 07:36 AM)jacklee26 Wrote: i have one more question what if my first second row is =====, how to able to implement, like this:
[inline]
atettime Tput Tput2
====== ===== ========
20221018.165317.401555 0.000545 0.000545
20221018.165327.401723 0.001422 0.000545
[/inline]
i run it occur error:
ValueError: could not convert string to float: '===='
This is why you don't want to use a solution where you assume certain cells will have certain values. My example doesn't care if you add columns or a row of "====" into the mix. It will pass the "====" through as text. If you don't want that in the spreadsheet, skip it.
    with open(src, "r") as src:
        # First line is column headings
        line = next(src).split()
        sheet.append(line)
        # Next line is thrown away
        next(src)
Reply
#13
(Nov-09-2022, 03:25 PM)deanhystad Wrote:
(Nov-09-2022, 07:36 AM)jacklee26 Wrote: i have one more question what if my first second row is =====, how to able to implement, like this:
[inline]
atettime Tput Tput2
====== ===== ========
20221018.165317.401555 0.000545 0.000545
20221018.165327.401723 0.001422 0.000545
[/inline]
i run it occur error:
ValueError: could not convert string to float: '===='
This is why you don't want to use a solution where you assume certain cells will have certain values. My example doesn't care if you add columns or a row of "====" into the mix. It will pass the "====" through as text. If you don't want that in the spreadsheet, skip it.
    with open(src, "r") as src:
        # First line is column headings
        line = next(src).split()
        sheet.append(line)
        # Next line is thrown away
        next(src)

HI deanhystad,
why I use the code
import pandas as pd
please refer the attached picture 


with open("result.txt", "r") as file:
    column_names = next(file).split()
    df = pd.read_csv(file, sep=" ", names=column_names)
df.to_excel("test.xlsx", index=False)
The datetime, or column did not write into excel file
For example, file is

datettime Tput RbNum MCS PdschBler nonWPdschBler
20221108.181509.814817 0.000091 2.5 9.0 0.0 25.0
20221108.181519.814948 0.000131 1.9 9.0 0.0 50.0

But in excel write like this
datettime Tput RbNum MCS PdschBler nonWPdschBler
0.000091 2.5 9.0 0.0 25.0
0.000131 1.9 9.0 0.0 50.0

Attached Files

Thumbnail(s)
   
Reply
#14
Interesting. I cut your example data and pasted into a file named result.txt. I cut the program and pasted it into a file named test.py. I ran the program and it generated test.xlsx. I opened test.xlsx and it looks like this:

Output:
datettime Tput RbNum MCS PdschBler nonWPdschBler 20221108.181509.814817 0.00 2.50 9 0 25 20221108.181519.814948 0.00 1.90 9 0 50
And because the header row doesn't have a bunch of extra spaces between the column headers, the program can be even simpler.
import pandas as pd
 
with open("result.txt", "r") as file:
    df = pd.read_csv(file, sep=" ")
df.to_excel("test.xlsx", index=False)
Reply


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