Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python-xlsxwriter only writes as a text when using csv data

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
like image 343
user3130288 Avatar asked Dec 23 '13 17:12

user3130288


1 Answers

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.

like image 72
jmcnamara Avatar answered Oct 17 '22 03:10

jmcnamara