python保存为xlsb_Python XLSB到CSV的转换数据类型
我一直致力于創建一個腳本,將Excel文件轉換成csv,以便在我們的一些工具中進行進一步處理。在
對于xls、xlsx和xlsm,我找到了將文件作為文本對象打開或使用pandas的解決方案。當遇到xlsb文件時,我在pyxlsb包上登陸,這個包可以工作。。。請參閱此處使用的代碼How can I convert a XLSB file to csv using python?
遺憾的是,我注意到xlsb文件中的int()值似乎被轉換為float,并因此以float的形式寫入csv文件。這顯然是不可取的。在
基本上我在尋找一個dtype=對象類型的解決方案。我想也許我可以用另一個函數把值轉換回int。不過,我認為這樣做效率低下,而且容易出錯。在
我的代碼:to_replace_list = ['\r', '\n', '\\r\\n', '\\' + str(out_del)] # values to replace in all rows
with open_xlsb(file_to_convert) as wb: # open xlsb file using pyxlsb package
for sheet_name in wb.sheets: # loop over all sheets in file
with wb.get_sheet(sheet_name) as sheet: # open xlsb sheet obj
out_file = out_filer(total_filename, sheet_name) # define output file name based on source and sheet
with open(out_file, 'a') as o: # open output csv obj
for row in sheet.rows(): # loop over rows in xlsb obj
print([re.sub(value, '', str(cell.v)) for value in to_replace_list for cell in row])
sidequest:xlsb文件中的空值在輸出中將被稱為None。我希望這個是“”。在
總結
以上是生活随笔為你收集整理的python保存为xlsb_Python XLSB到CSV的转换数据类型的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: html页面实现自动刷新或自动跳转
- 下一篇: Springboot 我随手封装了一个万