Search code examples
pythoncsvopenpyxlxlsx

I can not keep within 1 second


Instead of 1000 ms, my code fits in 1014 Please help me with the optimization

Condition:

In the Excel office program (or its analogues), you can always save the table in csv format. Imagine that this functionality is broken. Write your own excel -> csv transcoder using the appropriate libraries. Your program should open the data.xlsx file containing arbitrary data and save the result in output.csv

Input format: Not

Output format: In the output.csv file, output data from the source file in csv format. Separator - "semicolon" Quotation - quotation marks

Notes: Use the csv module

My code

import csv
import openpyxl
from openpyxl import Workbook


wb = openpyxl.load_workbook(filename='data.xlsx', data_only=True)
with open('output.csv', 'w', newline='', encoding="utf8") as csvfile:
    writer = csv.writer(csvfile, delimiter=';', quotechar='"')
    ws = wb.active
    rows = []
    for row in ws.values:
        w = map(lambda x: float(x) if x is not None and type(x) != str else x, row)   
        rows.append(w)
    writer.writerows(rows)

you need to optimize the code, but so that all the functionality remains

Sample tests:

enter image description here

Note: These lines of code are responsible for preserving the decimal places of float numbers, converted to integers for recording in a file

w = map(lambda x: float(x) if x is not None and type(x) != str else x, row)

they cannot be omitted

link to test xlsx file: https://yadi.sk/i/bF-5kOMTuyuMMg


Solution

  • The float conversion can be slightly faster with:

    w = map(lambda x: float(x) if type(x) is int else x, row)