[英]Python: how to change cell num type using xlsxwriter
I am using python to convert my .csv file to xlsx files , file conversion happens successfully. 我正在使用python将我的.csv文件转换为xlsx文件 ,文件转换成功完成。 At the same time I would like to change the cell num type also, like where ever there are float type xx.xxxx, that will be reduced to .xx (2 decimals) values.
同时,我也想更改单元格num类型,就像有浮点类型xx.xxxx的地方一样,它将被减小为.xx(两位十进制)的值。
input test.csv sheet, seperated by "," 输入test.csv表,以“,”分隔
Name Date Total request sas rate
rasta777 16062017 387865 89.3456
rasta777 16062017 291293 67.675
rasta777 16062017 256418 100
rasta777 16062017 251558 98.98
I have already tried the below python script using xlsxwriter
, but getting an error. 我已经使用
xlsxwriter
尝试了以下python脚本,但出现错误。
import os
import glob
import csv
from xlsxwriter.workbook import Workbook
for csvfile in glob.glob(os.path.join('.', 'hourly.csv')):
workbook = Workbook(csvfile + '.xlsx')
worksheet = workbook.add_worksheet()
format = workbook.add_format()
format.set_bold()
format.set_font_color('blue')
format.set_num_format('0x02')
format1 = workbook.add_format({'font_color': 'blue'})
with open(csvfile, 'rb') as f:
reader = csv.reader(f)
for r, row in enumerate(reader):
for c, col in enumerate(row):
worksheet.write(r, c, col)
worksheet.set_row(0, 20, format)
worksheet.set_column('A:P', format)
workbook.close()
getting error that 得到的错误
"TypeError: unsupported operand type(s) for *: 'Format' and 'int'"
“ TypeError:*:'格式'和'整数'不支持的操作数类型”
My second problem is generated output (getting output if I remove worksheet.set_column('A:P', format)
) gives cell type as text, whereas my data is all of numerical values except headers. 我的第二个问题是生成输出(如果删除
worksheet.set_column('A:P', format)
,则获得输出)将单元格类型设置为文本,而我的数据是除标头外的所有数值。
Last time I checked XlsxWriter's worksheet
did not support changing the cell type after the value was written. 上次我检查XlsxWriter的
worksheet
不支持在写入值后更改单元格类型。 When you want to write a value of specific type, you have to write it using a method specific to a type, like so: 当您要编写特定类型的值时,必须使用特定于该类型的方法来编写它,如下所示:
worksheet.write_datetime(idx, fld_idx, fld, date_format)
You also have to specify format while writing and I don't think it's possible to change the format after the fact. 您还必须在编写时指定格式, 但我认为事后不能更改格式。
Generally, I've learned by experience with XlsxWriter that it's better to prepare everything you need before writing and then write the cell once with all the correct values. 通常,我从XlsxWriter的经验中学到,最好在编写之前准备好所需的所有内容,然后使用所有正确的值一次编写单元格。 So eg I use the
date_format
predefined the usual way while writing the datetime
to the cell. 因此,例如,在将
datetime
时间写入单元格时,我通常使用预定义的date_format
。 That works. 这样可行。
Read up on type-specific methods here: http://xlsxwriter.readthedocs.io/worksheet.html , in particular: 在此处阅读特定于类型的方法: http : //xlsxwriter.readthedocs.io/worksheet.html ,尤其是:
write_string()
write_number()
write_blank()
write_formula()
write_datetime()
write_boolean()
write_url()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.