简体   繁体   English

Python-xlsxwriter仅在使用csv数据时作为文本写入

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

I am trying to create an excel file using python's xlsxwriter package. 我正在尝试使用python的xlsxwriter包创建一个excel文件。 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. 为此,我使用csv包来读取csv / txt文件。 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. 但是,当我从csv中读取数据并将其输出到xlsx文件时,所有数字/日期/等都将作为文本格式化。 I believe this is because when I read the file in each data point is surround by single quotes (ex: '00082424'). 我相信这是因为当我读取每个数据点中的文件时,用单引号括起来(例如:'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. 这导致excel将其作为文本读取(它甚至抛出其中一个小注释错误,表示它看起来像一个前面带有'的数字),因此保持前导零。 How can I read my data in from a csv and export it using xlsxwriter such that everything is not preceded by a '? 如何从csv中读取我的数据并使用xlsxwriter将其导出,以便所有内容前面都没有'?

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

By default XlsxWriter writes Python string data as Excel strings. 默认情况下,XlsxWriter将Python string数据写为Excel字符串。

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: 但是,对于这样的情况,当从csv或文本文件中读取数据总是字符串时,有一个构造函数选项将数字字符串转换为Excel数字:

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

See the XlsxWriter constructor and write() docs for more details. 有关更多详细信息,请参阅XlsxWriter 构造函数write()文档。

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: 将csv行压缩到一起后,如果值包含所有数字,则可以使用列表推导将每列中的值转换为整数:

Contents of csv_test.csv: 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. 如您所见,'after'输出是一个列表,其中数字已转换为整数,并且不再有引号。 You should now be able to write that list using xlsxwriter and the integers will be brought in in the correct format. 您现在应该能够使用xlsxwriter编写该列表,并且将以正确的格式引入整数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM