0

I have a large dataset, which consists of both floats, ints, and strings in cells. My original data set is csv format. when converting to xlsx, I get numbers stored as text error.

I have seen this which gives script if you are manually writing to cells.

as well as this which shows how to convert csv to text.

This is my splicing of the two scripts:

import csv
import os
import glob
import xlsxwriter

from xlsxwriter import Workbook
workbook = xlsxwriter.Workbook('file.xlsx', {'strings_to_numbers': True})

for csvfile in glob.glob(os.path.join('.', '*.csv')):
    workbook = Workbook(csvfile[:-4] + '.xlsx')
    worksheet = workbook.add_worksheet()
    with open(csvfile, 'rt', encoding='utf8') as f:
        reader = csv.reader(f)
        for r, row in enumerate(reader):
            for c, col in enumerate(row):
                worksheet.write(r, c, col)

workbook.close()

Which doesn't work for resolving the numbers stored as text issue. That issue persists.

I want to write numbers as numbers as it is converting the csv to xlsx

Mel
  • 174
  • 1
  • 11
  • 1
    Can you also post some sample data that is giving you an issue? That way we can recreate with your exact problem. – cwalvoort May 28 '19 at 22:14

1 Answers1

1

The first line of your for loop is overriding the workbook variable that you have defined using the "strings_to_numbers". Try removing that line since it looks like you want to add worksheets to a single workbook, correct?

Based on the examples you linked, it looks like this is just a small copy/paste error :).

cwalvoort
  • 1,851
  • 1
  • 18
  • 19