我从SAP应用程序下载了一些销售数据集。 SAP已自动将数据转换为.XLS文件。每当我使用Pandas
库打开它时,我收到以下错误:
XLRDError: Unsupported format, or corrupt file: Expected BOF record; found '\xff\xfe\r\x00\n\x00\r\x00'
当我使用MSEXCEL打开.XLS文件时,会显示一个弹出窗口,说明file is corrupt or unsupported extension do you want to continue
点击“是”'它显示正确的数据。当我使用msexcel将文件再次保存为.xls时,我可以使用Pandas
来使用它。
所以,我尝试使用os.rename()
重命名文件,但它有用。我尝试打开文件并删除\xff\xfe\r\x00\n\x00\r\x00
,但随后它也工作了。
解决方案是打开MSEXCEL并再次以.xls手动保存文件,有没有办法自动执行此操作。请帮助。
答案 0 :(得分:2)
最后,我将损坏的.xls
转换为正确的.xls
文件。以下是代码:
# Changing the data types of all strings in the module at once
from __future__ import unicode_literals
# Used to save the file as excel workbook
# Need to install this library
from xlwt import Workbook
# Used to open to corrupt excel file
import io
filename = r'SALEJAN17.xls'
# Opening the file using 'utf-16' encoding
file1 = io.open(filename, "r", encoding="utf-16")
data = file1.readlines()
# Creating a workbook object
xldoc = Workbook()
# Adding a sheet to the workbook object
sheet = xldoc.add_sheet("Sheet1", cell_overwrite_ok=True)
# Iterating and saving the data to sheet
for i, row in enumerate(data):
# Two things are done here
# Removeing the '\n' which comes while reading the file using io.open
# Getting the values after splitting using '\t'
for j, val in enumerate(row.replace('\n', '').split('\t')):
sheet.write(i, j, val)
# Saving the file as an excel file
xldoc.save('myexcel.xls')
import pandas as pd
df = pd.ExcelFile('myexcel.xls').parse('Sheet1')
没有错误。
答案 1 :(得分:1)
解决此问题的另一种方法是使用win32com.client库:
import win32com.client
import os
o = win32com.client.Dispatch("Excel.Application")
o.Visible = False
filename = os.getcwd() + '/' + 'SALEJAN17.xls'
output = os.getcwd() + '/' + 'myexcel.xlsx'
wb = o.Workbooks.Open(filename)
wb.ActiveSheet.SaveAs(output,51)
在我的示例中,您保存为.xlsx格式,但也可以另存为.xls。