Solve xlrd.biffh.XLRDError: Unsupported format, or corrupt file: Expected BOF record; found bDebug is

Table of Contents

Solve xlrd.biffh.XLRDError: Unsupported format, or corrupt file: Expected BOF record; found bDebug is

introduce

wrong reason

solution

1. Check Excel file format

2. Check whether the Excel file is damaged

3. Check how the Excel file is opened

4. Update xlrd library version

Sample code

in conclusion

scene description

Sample code

Introduction to xlrd library

Install xlrd library

Features of xlrd

Reading Excel files using xlrd


Solve xlrd.biffh.XLRDError: Unsupported format, or corrupt file: Expected BOF record; found bDebug is

Introduction

When performing data processing and analysis, we often need to read and parse Excel files. And xlrd is a commonly used Python library for reading Excel files. However, sometimes when we use xlrd to read Excel files, we may encounter errors such as “xlrd.biffh.XLRDError: Unsupported format, or corrupt file: Expected BOF record; found b’Debug is”. This error usually means that the Excel file format is not supported or the file is corrupted.

Error reason

There are several possible causes for this error, including:

  • The format of the Excel file is not supported, for example, it contains encryption, password protection, or unknown file types;
  • The Excel file itself is damaged or incomplete;
  • The Excel file was opened incorrectly.

Solution

For different reasons, we can take some of the following solutions to solve this error.

1. Check Excel file format

First check whether the format of the Excel file is supported. The Excel file formats supported by the xlrd library are xls and xlsx. If your file is in another format, such as xlsm, this error may occur. You can check the format of a file using the following code:

pythonCopy codeimport xlrd
print(xlrd.open_workbook("your_file.xlsx").format)

If the output value is 0, it means that it is in xls format. If the output value is 1, it means that it is in xlsx format. If the output value is anything else, the file format is not supported.

2. Check whether the Excel file is damaged

The Excel file may be corrupted or incomplete, rendering xlrd unreadable. We can try to use other tools or software to open the Excel file to determine whether the file is normal. If the file is damaged, you need to find the corresponding backup file or repair the file.

3. Check how the Excel file is opened

Sometimes when we read an Excel file, the file is still open. In this case, the Excel file may appear in read-only mode or may not be fully accessible. We can try to close the Excel file and then try to read the file using the xlrd library.

4. Update xlrd library version

Different versions of the xlrd library may have some bugs or compatibility issues. You can try to use the latest version of the xlrd library to replace the current version, and then try to read the Excel file.

sample code

The following is a sample code that demonstrates how to handle the “xlrd.biffh.XLRDError: Unsupported format, or corrupt file: Expected BOF record; found b’Debug is” error.

pythonCopy codeimport xlrd
def read_excel_file(filename):
    try:
        workbook = xlrd.open_workbook(filename)
        # Continue processing the logic of Excel files
    except xlrd.biffh.XLRDError as e:
        print(f"Error reading Excel file: {e}")
        print("Please try the following solutions:")
        print("1. Check whether the file format is supported")
        print("2. Check whether the file is damaged")
        print("3. Check how the file is opened")
        print("4. Update xlrd library version")
#Read Excel file
read_excel_file("your_file.xlsx")

Conclusion

When we encounter the error “xlrd.biffh.XLRDError: Unsupported format, or corrupt file: Expected BOF record; found b’Debug is”, we need to check the format of the Excel file, whether the file is damaged, how the file is opened, and xlrd Library version and other factors. With the above solutions, we can better handle this error and successfully read and parse the Excel file. I hope this article can help you solve the “xlrd.biffh.XLRDError: Unsupported format, or corrupt file: Expected BOF record; found b’Debug is” error. If you have any questions or suggestions, please feel free to ask. Thanks for reading!

scene description

Suppose we need to read students’ grades from an Excel file and calculate their average score. The data format in the Excel file is as follows:

student ID

Name

Math scores

English results

Chinese language scores

001

Zhang San

90

85

92

002

John Doe

78

82

88

003

Wang Wu

85

90

88

We need to use the xlrd library to read the Excel file and calculate the average score of each student.

Example code

pythonCopy codeimport xlrd
def calculate_average_score(filename):
    try:
        workbook = xlrd.open_workbook(filename)
        sheet = workbook.sheet_by_index(0) # Read the first worksheet by default
        num_rows = sheet.nrows # Get the number of rows
        total_scores = 0
        num_students = num_rows - 1 # Subtract header rows
        #Loop through each student’s grade rows
        for row in range(1, num_rows):
            math_score = sheet.cell_value(row, 2)
            english_score = sheet.cell_value(row, 3)
            chinese_score = sheet.cell_value(row, 4)
            total_scores + = math_score + english_score + chinese_score
        average_score = total_scores / (num_students * 3) # Calculate the average score
        return average_score
    except xlrd.biffh.XLRDError as e:
        print(f"Error reading Excel file: {e}")
        return None
# Read and calculate the average score of students' grades in the Excel file
average_score = calculate_average_score("student_scores.xlsx")
if average_score is not None:
    print(f"Students’ average score: {average_score}")

In the above sample code, we define a ??calculate_average_score(filename)?? function, which reads the student performance data in the Excel file through the xlrd library and calculates the average score of all students. First, we open the file and select the first worksheet (index 0). Then, use the ??nrows?? property to get the number of rows, that is, the number of students. Next, we use a loop to loop through each student’s grade row, use the ??cell_value(row, column)?? method to get the value of each cell, and accumulate the grades for each subject to ??total_scores??. Finally, we calculate the average score and return it. In the main program, we call the ??calculate_average_score(filename)?? function and print out the student’s average score. If an ??xlrd.biffh.XLRDError?? error occurs, we will capture and print the error message and return ??None??. I hope this sample code can help you use the xlrd library to read and process Excel file data in practical applications. If you have any questions or suggestions, please feel free to ask. Thanks for reading!

xlrd library introduction

xlrd is a Python library for reading Excel files. It can conveniently read data in Excel files, including worksheets, cell contents, formats, etc.

Install xlrd library

Before using the xlrd library, we need to install it first. It can be installed using pip with the following command:

plaintextCopy codepip install xlrd

Features of xlrd

  • Supports reading multiple worksheets in Excel files.
  • You can get the properties of the worksheet, such as name, number of rows, number of columns, etc.
  • You can get the cell value, data type, format, etc.
  • Compatible with Excel files that support xls file format (xlsx file format is not supported).

Use xlrd to read Excel files

The following introduces several commonly used xlrd classes and methods to read data from Excel files.

  1. ?open_workbook(filename)?
  • Function: Open Excel file and create a Workbook object.
  • Parameters: ??filename?? – Excel file name, which can be a relative path or an absolute path to the file.
  • Return value: Workbook object.
  1. ?sheet_names()?
  • Function: Get the list of worksheet names.
  • Return value: A list containing worksheet names.
  1. ?sheet_by_name(sheet_name)?
  • Function: Get the specified worksheet based on the worksheet name.
  • Parameters: ??sheet_name?? – sheet name.
  • Return value: Sheet object.
  1. ?sheet_by_index(sheet_index)?
  • Function: Get the specified worksheet based on the worksheet index.
  • Parameters: ??sheet_index?? – Worksheet index, starting from 0.
  • Return value: Sheet object.
  1. ?nrows?
  • Function: Get the total number of rows in the worksheet.
  • Return value: The total number of rows in the worksheet.
  1. ?ncols?
  • Function: Get the total number of columns of the worksheet.
  • Return value: The total number of columns in the worksheet.
  1. ?cell_value(rowx, colx)?
  • Function: Get the data of the specified cell.
  • parameter:
  • ??rowx?? – row index, starting from 0.
  • ??colx?? – Column index, starting from 0.
  • Return value: the data of the specified cell. Here is a sample code that uses xlrd to read an Excel file:
pythonCopy codeimport xlrd
#Open Excel file
workbook = xlrd.open_workbook("student_scores.xlsx")
# Get the first worksheet
sheet = workbook.sheet_by_index(0)
# Get the total number of rows and columns
num_rows = sheet.nrows
num_cols = sheet.ncols
# Traverse each row of data
for row in range(num_rows):
    # Get the value of each row
    row_data = []
    for col in range(num_cols):
        cell_value = sheet.cell_value(row, col)
        row_data.append(cell_value)
    print(row_data)

In the above code, we first use the ??open_workbook()?? method to open an Excel file and create a Workbook object. Then, use the sheet_by_index() method to select the first worksheet, using nrows and ncols ?Properties get the total number of rows and columns of the worksheet. Iterate through each row of data through a two-level loop, use the ??cell_value()?? method to get the data of each cell and add it to a list. Finally, print each line of data. I hope the above introduction and sample code can help you understand and use the xlrd library to read data in Excel files. If you have any questions, please feel free to ask.

The knowledge points of the article match the official knowledge files, and you can further learn related knowledge. Java Skill TreeHomepageOverview 137919 people are learning the system