[转载]使用XLRD读取数据

本文介绍了如何使用Python的xlrd模块来操作Excel文件。主要内容包括xlrd模块的安装步骤、基本使用方法,如读取Excel文件、获取指定单元格的值、遍历行列表等,并提供了实用的代码示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

www.cnblogs.com/lhj588/archive/2012/01/06/2314181.html

一、安装xlrd模块

   到python官网下载http://pypi.python.org/pypi/xlrd模块安装,前提是已经安装了python 环境。

二、使用介绍

  1、导入模块

      import xlrd

   2、打开Excel文件读取数据

      data = xlrd.open_workbook('excelFile.xls')

   3、使用技巧

       获取一个工作表

 

        table = data.sheets()[0]          #通过索引顺序获取
 
        table = data.sheet_by_index(0) #通过索引顺序获取

 

        table = data.sheet_by_name(u'Sheet1')#通过名称获取
 
       获取整行和整列的值(数组)
   
         table.row_values(i)
 
         table.col_values(i)
 
       获取行数和列数
  
        nrows = table.nrows
 
        ncols = table.ncols
       
        循环行列表数据
        for i in range(nrows ):
      print table.row_values(i)
 
单元格
cell_A1 = table.cell(0,0).value
 
cell_C4 = table.cell(2,3).value
 
使用行列索引
cell_A1 = table.row(0)[0].value
 
cell_A2 = table.col(1)[0].value
 
简单的写入
row = 0
 
col = 0
 
# 类型 0 empty,1 string, 2 number, 3 date, 4 boolean, 5 error
ctype = 1   value = '单元格的值'
 
xf = 0  # 扩展的格式化
 
table.put_cell(row, col, ctype, value, xf)
 
table.cell(0,0)  #单元格的值'
 
table.cell(0,0).value #单元格的值'
 

 

三、Demo代码

   Demo代码其实很简单,就是读取Excel数据。

   

复制代码
 1 # -*- coding: utf-8 -*- 
 2 import  xdrlib ,sys
 3 import xlrd
 4 def open_excel(file= 'file.xls'):
 5     try:
 6         data = xlrd.open_workbook(file)
 7         return data
 8     except Exception,e:
 9         print str(e)
10 #根据索引获取Excel表格中的数据   参数:file:Excel文件路径     colnameindex:表头列名所在行的所以  ,by_index:表的索引
11 def excel_table_byindex(file= 'file.xls',colnameindex=0,by_index=0):
12     data = open_excel(file)
13     table = data.sheets()[by_index]
14     nrows = table.nrows #行数
15     ncols = table.ncols #列数
16     colnames =  table.row_values(colnameindex) #某一行数据 
17     list =[]
18     for rownum in range(1,nrows):
19 
20          row = table.row_values(rownum)
21          if row:
22              app = {}
23              for i in range(len(colnames)):
24                 app[colnames[i]] = row[i] 
25              list.append(app)
26     return list
27 
28 #根据名称获取Excel表格中的数据   参数:file:Excel文件路径     colnameindex:表头列名所在行的所以  ,by_name:Sheet1名称
29 def excel_table_byname(file= 'file.xls',colnameindex=0,by_name=u'Sheet1'):
30     data = open_excel(file)
31     table = data.sheet_by_name(by_name)
32     nrows = table.nrows #行数 
33     colnames =  table.row_values(colnameindex) #某一行数据 
34     list =[]
35     for rownum in range(1,nrows):
36          row = table.row_values(rownum)
37          if row:
38              app = {}
39              for i in range(len(colnames)):
40                 app[colnames[i]] = row[i]
41              list.append(app)
42     return list
43 
44 def main():
45    tables = excel_table_byindex()
46    for row in tables:
47        print row
48 
49    tables = excel_table_byname()
50    for row in tables:
51        print row
52 
53 if __name__=="__main__":
54     main()
复制代码

<think>我们之前使用了pandas的read_excel来读取Excel文件,而xlrd是另一个常用的库,特别是在处理旧版Excel文件(.xls)时。但是注意,xlrd在2.0.0版本之后不再支持.xlsx文件(只支持.xls)。因此,我们通常使用openpyxl或pandas(内部使用xlrd或openpyxl)来处理.xlsx文件。 不过,如果你特别要求使用xlrd,我们可以修改文件读取部分。但请注意,如果文件是.xlsx格式,我们需要确保安装的是xlrd<2.0.0(支持.xlsx)或者使用openpyxl。 考虑到兼容性,我们通常更推荐使用pandas,因为它可以自动根据文件格式选择引擎。但为了满足要求,下面将展示如何使用xlrd读取Excel文件。 步骤: 1. 检查文件扩展名,如果是.xls,使用xlrd;如果是.xlsx,使用openpyxl或者xlrd(如果版本<2.0.0)?但是为了避免版本问题,我们这里统一使用xlrd(仅用于.xls)和openpyxl(用于.xlsx)?但这样会增加复杂性。 另一种做法是:我们使用xlrd来读取文件,但要求用户安装兼容版本的xlrd(即1.2.0版本,可以同时支持.xls和.xlsx)。但是,xlrd在1.2.0版本后停止了对.xlsx的支持,所以如果我们要用xlrd读取.xlsx,必须安装1.2.0或更早版本。然而,这并不推荐,因为新版本的xlrd只支持.xls。 因此,我们建议:如果文件是.xls,使用xlrd;如果是.xlsx,使用openpyxl。或者,我们可以使用pandas,它会自动处理引擎问题。 但是,题目要求使用xlrd,所以我们假设文件是.xls格式,或者安装的是xlrd 1.2.0(支持.xlsx)。 然而,为了代码的健壮性,我们最好还是根据文件扩展名来选择引擎。但这里我们按照要求,使用xlrd来读取,并假设用户安装的是xlrd 1.2.0(可以读取.xls和.xlsx)或者文件是.xls格式。 修改文件加载函数,使用xlrd: 注意:xlrd读取Excel文件后,我们需要手动将其转换为numpy数组。 但是,由于xlrd使用相对底层,我们可能需要自己遍历行和列。下面我们写一个函数,用xlrd读取Excel文件并提取前两列数据。 但是,如果文件很大,使用xlrd可能比pandas慢,因为我们需要逐行读取。 另外,我们也可以使用xlrd读取整个工作表然后转换为数组。 步骤: 1. 打开工作簿 2. 取第一个工作表 3. 读取所有行,提取前两列(假设第一行是标题,第二行开始是数据) 但是,我们之前的代码假设第一行是标题,所以这里我们也跳过第一行。 我们修改`load_mirror_positions`函数,当使用xlrd时: 但是,由于我们之前已经有一个智能查找文件的函数,现在我们在找到文件后,用xlrd读取。 不过,考虑到用户可能没有安装xlrd,我们应该在代码中检查并提示安装。 所以,我们修改后的文件加载函数如下:</think>### 使用 xlrd 库读取 Excel 附件数据 `xlrd` 是一个专门用于读取 Excel 文件的 Python 库,尤其适合处理 `.xls` 格式的文件。以下是如何使用 `xlrd` 替代 `pandas` 来读取附件数据的完整解决方案: ```python import xlrd import numpy as np import os import sys import glob def load_mirror_positions_with_xlrd(): """使用 xlrd 加载定日镜位置数据""" # 查找可能的附件文件 file_path = find_mirror_data_file() if not file_path: print("警告: 未找到附件文件,使用示例数据") return generate_sample_data() print(f"找到定日镜数据文件: {file_path}") try: # 打开工作簿 workbook = xlrd.open_workbook(file_path) # 取第一个工作表 sheet = workbook.sheet_by_index(0) # 确定数据列的位置 x_col, y_col = find_coordinate_columns(sheet) # 提取坐标数据 mirror_positions = [] for row_idx in range(1, sheet.nrows): # 跳过标题行 try: x_val = sheet.cell_value(row_idx, x_col) y_val = sheet.cell_value(row_idx, y_col) # 确保值是数字 if isinstance(x_val, (int, float)) and isinstance(y_val, (int, float)): mirror_positions.append([x_val, y_val]) except: continue # 跳过格式错误的行 if not mirror_positions: print("警告: 文件中未找到有效坐标数据,使用示例数据") return generate_sample_data() print(f"成功读取 {len(mirror_positions)} 个定日镜位置") return np.array(mirror_positions) except xlrd.XLRDError as e: print(f"xlrd 读取错误: {e}") except Exception as e: print(f"读取文件时出错: {e}") print("警告: 文件读取失败,使用示例数据") return generate_sample_data() def find_mirror_data_file(): """查找可能的定日镜位置数据文件""" # 尝试的文件名模式 patterns = [ '附件.xlsx', '附件.xls', '定日镜位置.xlsx', 'mirror_positions.xlsx', 'heliostat_data.xlsx', 'data.xlsx', 'input_data.xlsx', '*.xls', '*.xlsx' ] # 尝试所有可能的文件 for pattern in patterns: matches = glob.glob(pattern) if matches: return matches[0] # 返回第一个匹配的文件 # 没有找到文件 return None def find_coordinate_columns(sheet): """确定坐标列的位置""" # 可能的列名 possible_names = ['x', 'y', '坐标x', '坐标y', 'x坐标', 'y坐标', 'position_x', 'position_y'] # 检查第一行标题 header_row = sheet.row_values(0) # 尝试匹配列名 x_col = None y_col = None for col_idx, header in enumerate(header_row): header_lower = str(header).lower() if 'x' in header_lower and not y_col: # 避免将同一个列同时识别为x和y x_col = col_idx elif 'y' in header_lower and not x_col: y_col = col_idx # 如果找到两个列,返回结果 if x_col is not None and y_col is not None: return x_col, y_col # 如果只找到一个列,尝试寻找另一个 if x_col is not None: # 尝试下一个列作为y if x_col + 1 < sheet.ncols: y_col = x_col + 1 return x_col, y_col # 默认使用前两列 print("警告: 无法确定坐标列,使用前两列作为坐标") return 0, 1 def generate_sample_data(): """生成示例数据作为后备方案""" num_mirrors = 200 angles = np.linspace(0, 2 * np.pi, num_mirrors, endpoint=False) radii = np.linspace(120, 300, num_mirrors) return np.array([radii * np.cos(angles), radii * np.sin(angles)]).T # ================================================ # 在问题1求解函数中使用 # ================================================ def solve_problem1(): # ...(其他参数设置不变)... # 加载定日镜位置 - 使用 xlrd mirror_positions = load_mirror_positions_with_xlrd() # ...(后续计算不变)... ``` ### 代码详细解释: #### 1. `load_mirror_positions_with_xlrd()` 函数 这是核心的数据读取函数,使用 xlrd 库处理 Excel 文件: ```python def load_mirror_positions_with_xlrd(): # 查找文件 file_path = find_mirror_data_file() # 打开工作簿 workbook = xlrd.open_workbook(file_path) # 取第一个工作表 sheet = workbook.sheet_by_index(0) # 确定坐标列位置 x_col, y_col = find_coordinate_columns(sheet) # 提取坐标数据 mirror_positions = [] for row_idx in range(1, sheet.nrows): # 从第2行开始(跳过标题) x_val = sheet.cell_value(row_idx, x_col) y_val = sheet.cell_value(row_idx, y_col) mirror_positions.append([x_val, y_val]) return np.array(mirror_positions) ``` #### 2. `find_coordinate_columns()` 函数 智能识别坐标列的位置: ```python def find_coordinate_columns(sheet): # 可能的列名 possible_names = ['x', 'y', '坐标x', '坐标y', 'x坐标', 'y坐标'] # 检查标题行 header_row = sheet.row_values(0) # 尝试匹配列名 for col_idx, header in enumerate(header_row): header_lower = str(header).lower() if any(name in header_lower for name in possible_names): if 'x' in header_lower: x_col = col_idx elif 'y' in header_lower: y_col = col_idx # 如果找到两个列,返回结果 if x_col is not None and y_col is not None: return x_col, y_col # 默认使用前两列 return 0, 1 ``` #### 3. 健壮性处理 代码包含多层错误处理和后备方案: - 文件查找失败时使用示例数据 - 列名识别失败时使用前两列 - 数据类型检查确保坐标是数字 - 异常捕防止程序崩溃 ### 安装 xlrd使用前需要安装 xlrd 库: ```bash pip install xlrd ``` ### 使用注意事项: 1. **文件格式支持**: - xlrd 主要支持 `.xls` 格式 - 对于 `.xlsx` 格式,建议使用 `openpyxl` 或 `pandas` 2. **数据类型处理**: - xlrd 返回的数据类型可能是 float 或 string - 代码中进行了类型检查确保数值有效 3. **性能考虑**: - 对于大型 Excel 文件,xlrd 比 pandas 更节省内存 - 但在处理超大文件时可能较慢 4. **日期处理**: - xlrd 返回的日期是 Excel 的序列号格式 - 需要使用 `xlrd.xldate_as_datetime()` 转换 ### 替代方案:使用 openpyxl 处理 .xlsx 文件 如果附件是 .xlsx 格式,可以使用 openpyxl 替代 xlrd: ```python from openpyxl import load_workbook def load_with_openpyxl(file_path): """使用 openpyxl 加载 .xlsx 文件""" wb = load_workbook(filename=file_path, read_only=True) sheet = wb.active # 确定坐标列 x_col, y_col = find_coordinate_columns_openpyxl(sheet) mirror_positions = [] for row in sheet.iter_rows(min_row=2): # 跳过标题行 try: x_val = row[x_col].value y_val = row[y_col].value if isinstance(x_val, (int, float)) and isinstance(y_val, (int, float)): mirror_positions.append([x_val, y_val]) except: continue return np.array(mirror_positions) def find_coordinate_columns_openpyxl(sheet): """为 openpyxl 确定坐标列位置""" # 取第一行作为标题 headers = [cell.value for cell in sheet[1]] # 搜索坐标列 for idx, header in enumerate(headers): if header and isinstance(header, str): header_lower = header.lower() if 'x' in header_lower: x_col = idx elif 'y' in header_lower: y_col = idx return x_col, y_col ``` ### 完整集成方案 结合 xlrd 和 openpyxl 的解决方案: ```python def load_mirror_positions_universal(): """通用文件加载器,自动选择最佳库""" file_path = find_mirror_data_file() if not file_path: return generate_sample_data() # 根据文件扩展名选择加载器 if file_path.endswith('.xlsx'): try: from openpyxl import load_workbook return load_with_openpyxl(file_path) except ImportError: print("警告: 未安装 openpyxl,尝试使用 xlrd") try: import xlrd return load_mirror_positions_with_xlrd(file_path) except ImportError: print("错误: 需要安装 xlrd 或 openpyxl 来读取 Excel 文件") return generate_sample_data() ``` 这种方案会根据文件类型自动选择最佳库,并提供完整的错误处理。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值