我正在尝试打开为我的项目提供给我的excel文件,该excel文件是我们从SAP系统获得的文件。但是,当我尝试使用pandas打开它时,出现以下错误:


  XLRDError:不支持的格式或文件损坏:预期的BOF记录;找到了'\ xff \ xfe \ r \ x00 \ n \ x00 \ r \ x00'


以下是我的代码:

import pandas as pd
# To open an excel file
df = pd.ExcelFile('myexcel.xls').parse('Sheet1')

最佳答案

一旦对我有用,不知道它是否对您有用,但是无论如何,您可以尝试以下方法:

from __future__ import unicode_literals
from xlwt import Workbook
import io

filename = r'myexcel.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')

关于python - Python-XLRDError:不支持的格式,或文件损坏:预期的BOF记录,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/55356020/

10-10 14:05
查看更多