gpt4 book ai didi

Python - XLRDError : Unsupported format, 或损坏的文件:预期的 BOF 记录

转载 作者:行者123 更新时间:2023-12-01 08:09:23 25 4
gpt4 key购买 nike

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

XLRDError: Unsupported format, or corrupt file: Expected BOF record; found '\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 - XLRDError : Unsupported format, 或损坏的文件:预期的 BOF 记录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55356020/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com