Question

I am trying to create an excel file using python's xlsxwriter package. Currently I have all of the formatting done and all I need to do now is import the correct data. To do this I am using the csv package to read a csv/txt file. However when I read the data in from the csv and output it to the xlsx file all of the numbers/date/etc are fromatted as text. I believe this is because when I read the file in each data point is surround by single quotes (ex: '00082424'). This causes excel to read it as text (it even throws one of those little note errors saying it looks like a number preceded by a ') and as a result keeps the leading zeros. How can I read my data in from a csv and export it using xlsxwriter such that everything is not preceded by a '?

Here is an abridged version of the code I am using for the read-in and the output:

import csv
import xlsxwriter

""" Create a workbook and add worksheets """
workbook = xlsxwriter.Workbook('Test.xlsx')
worksheet1 = workbook.add_worksheet('Raw Data')

""" Read csv file """
with open("filename.txt") as filein:
    reader = csv.reader(filein, skipinitialspace = True)
    ind, c, d, e, f, g, h, i, j, k, l, m, n, o, p, q, r, s, t, u = zip(*reader)


""" Start from the cell C5. Rows and columns are zero indexed """
row = 4
col = 2

money_format = workbook.add_format({'num_format': '$#,##0'})

for d_dat in list(p):
    worksheet1.write(row, col, d_dat, money_format)
    row +=1

row = 4
Was it helpful?

Solution

By default XlsxWriter writes Python string data as Excel strings.

However, for cases like this, when reading from csv or text files where the data is always strings then there is a constructor option to convert numeric strings to Excel numbers:

workbook = xlsxwriter.Workbook('Test.xlsx', {'strings_to_numbers': True})

See the XlsxWriter constructor and write() docs for more details.

OTHER TIPS

Once you've zipped your csv rows together, you can use a list comprehension to convert the value in each column into an integer if the value contains all numbers:

Contents of csv_test.csv:

a,b,c,d,e,f
1,2,3,4,5,6
7,8,9,10,11,12

Script for type conversion:

import csv
with open('csv_test.csv') as ifile:
    reader = csv.reader(ifile)
    a,b,c,d,e,f = zip(*reader)
    f_type_con = [int(i) if i.isdigit() == True else i for i in f]
    print 'Here is column f, before: {}'.format(f)
    print 'Here is column f, after: {}'.format(f_type_con)

Output:

Here is column f, before: ('f', '6', '12')
Here is column f, after: ['f', 6, 12]

As you can see, the 'after' output is a list where the numbers have been converted to integers, and no longer have quotes around them. You should now be able to write that list using xlsxwriter and the integers will be brought in in the correct format.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top