Home > Back-end >  extra ' when writng numerics in excel in python
extra ' when writng numerics in excel in python

Time:08-11

I have the following code that reads some specific values from a file and writes them into an excel sheet. The code does what I want but adds an extra ' before the values extracted from the input file when writing them into an excel sheet.

Here is the code:

from openpyxl import Workbook

wb = Workbook()
ws = wb.active
column_cell_1 = 'A'
ws[column_cell_1 str(1)] = 'formula_size' 
column_cell_2 = 'B'
ws[column_cell_2 str(1)] = 'time' 

#
counter = 0
file = "nn.txt"
with open(file) as openfile:

    for line in openfile:
       s = line.split()
       for i,j in enumerate(s):
           if j == "total:":
              counter = counter   1
              print(s[i])
              total_time = s[i 1].split(",")[0]
              print(total_time)
              ws[column_cell_1 str(counter 1)].value = counter
              ws[column_cell_2 str(counter 1)].value = total_time

wb.save("nn.xlsx")

here how the output excel sheet looks like:

As you can see, there is an extra ' before the numeric value.

When I print the numeric value in the terminal, there is no ' before the numbers.

total:
0.5253981399982877
total:
1.5582128490004834
total:
7.660432515001958
total:
73.78555823400347

I would appreciate it if you can help me to avoid this extra ' in the excel sheet.

CodePudding user response:

The apostrophe marks the cell as being a string, which is exactly what you have written. If you want it treated as a number, then use float() to convert the item to a float.

  • Related