<output id="qn6qe"></output>

    1. <output id="qn6qe"><tt id="qn6qe"></tt></output>
    2. <strike id="qn6qe"></strike>

      亚洲 日本 欧洲 欧美 视频,日韩中文字幕有码av,一本一道av中文字幕无码,国产线播放免费人成视频播放,人妻少妇偷人无码视频,日夜啪啪一区二区三区,国产尤物精品自在拍视频首页,久热这里只有精品12

      Excel處理控件Aspose.Cells教程:如何將Excel區域轉換為Python列表

      在 Python 中處理 Excel 數據通常需要將特定的行和列提取為列表格式。將 Excel 范圍轉換為 Python 列表對于以下任務非常有用:

      • 使用PandasNumPy進行數據分析
      • 報告和 ETL 流程的自動化
      • 與機器學習模型或 API 集成

      在本教程中,我們將逐步學習如何借助Aspose.Cells在 Python 中將定義的 Excel 范圍轉換為列表。

      Aspose.Cells官方試用版免費下載,請聯系Aspose官方授權代理商慧都科技

      加入Aspose技術交流QQ群(1041253375),與更多小伙伴一起探討提升開發技能。

      Python Excel 到列表轉換庫

      開發人員無需手動解析 Excel 文件,而是可以使用 Aspose.Cells for Python via .NET(一個功能強大的 Excel 到列表轉換庫)。它不僅可以更輕松地將范圍、行和列提取到 Python 列表中,還支持公式、格式、圖表和數據透視表等高級功能,即使在復雜的電子表格中也能確保準確性。

      在編碼之前,請確保您的設置已準備就緒:

      1. 安裝Python 3.7+
      2. 從發行版下載 Aspose.Cells或使用 pip 安裝:
        pip install aspose-cells-python
      3. sample_data.xlsx準備一個包含以下內容的示例 Excel 文件( ):

      Aspose.Cells官方試用版免費下載,請聯系Aspose官方授權代理商慧都科技

      加入Aspose技術交流QQ群(1041253375),與更多小伙伴一起探討提升開發技能。

      將 Excel 范圍轉換為 Python 列表:分步指南

      讓我們了解使用 Aspose.Cells for Python 將一系列 Excel 數據轉換為 Python 列表的過程。

      按照以下步驟將 Excel 范圍轉換為 Python 中的列表:

      1. 首先,使用該類加載現有的 Excel 文件Workbook。
      2. 其次,獲取第一個工作表。
      3. 接下來,創建一個范圍,例如 A1 到 C4。
      4. 之后,將 Range 轉換為 Python List。
      5. 最后,打印列表。

      以下 Python 腳本加載 Excel 文件,定義范圍并將其轉換為 Python 列表。

      from aspose.cells import Workbook
      
      # Step 1: Load the Excel workbook
      book = cells.Workbook("sample_data.xlsx")
      
      # Step 2: Access the first worksheet
      sheet1 = book.worksheets.get(0)
      
      # Step 3: Define the range (A1:C4 in this example)
      sheet_cells = sheet1.cells
      range_obj = sheet_cells.create_range("A1", "C4")
      
      # Step 4: Convert the range into a nested Python list
      range_list = []
      for row_index in range(range_obj.first_row, range_obj.first_row + range_obj.row_count):
          row = []
          for column_index in range(range_obj.first_column, range_obj.first_column + range_obj.column_count):
              curr_cell = sheet_cells.check_cell(row_index, column_index)
              row.append(curr_cell.value if curr_cell else "")
          range_list.append(row)
      
      # Step 5: Print the Python list
      print("Python List Output:")
      print(range_list)

      輸出

      Python List Output:
      [['City', 'Region', 'Store'], ['Chicago', 'Central', 3055], ['New York', 'East', 3036], ['Detroit', 'Central', 3074]]

      此完整腳本演示了如何從 Excel 中提取數據并將其轉換為 Python 列表。之后,您可以根據需要輕松地將其轉換為 Pandas 或 JSON。

      將 Python 列表轉換為 Pandas DataFrame

      使用 Pandas,您可以直接將列表轉換為 DataFrame:

      import pandas as pd
      
      # Convert to a Pandas DataFrame
      df = pd.DataFrame(range_list[1:], columns=range_list[0])
      print(df)

      Pandas DataFrame 輸出:

             City   Region  Store
      0   Chicago  Central   3055
      1  New York     East   3036
      2   Detroit  Central   3074

      將 Python 列表保存為 JSON

      您還可以將數據導出為 JSON:

      import json
      
      # Convert to JSON
      json_output = json.dumps(range_list)
      print(json_output)

      JSON 輸出:

      [["City", "Region", "Store"], ["Chicago", "Central", 3055], ["New York", "East", 3036], ["Detroit", "Central", 3074]]

      在 Python 中將 Excel 行轉換為列表

      有時您可能只想從 Excel 中提取一行并將其存儲為列表。以下是使用 Aspose.Cells 的操作方法:

      1. 加載 Excel 工作簿。
      2. 訪問目標工作表。
      3. 通過索引選擇行。
      4. 將行值收集到 Python 列表中。
      # Import Aspose.Cells library
      from aspose.cells import Workbook
      
      # Step 1: Load the Excel workbook from file
      book = Workbook("sample_data.xlsx")
      
      # Step 2: Access the first worksheet in the workbook
      sheet = book.worksheets.get(0)
      
      # Step 3: Define the row index (0 = first row, which contains headers)
      row_index = 0
      cells = sheet.cells
      
      # Create a range object for the selected row
      row_range = cells.create_range(row_index, 0, 1, sheet.cells.max_column + 1)
      
      # Step 4: Convert the row into a Python list
      row_list = []
      for column_index in range(row_range.first_column, row_range.first_column + row_range.column_count):
          curr_cell = cells.check_cell(row_index, column_index)  # Get each cell in the row
          row_list.append(curr_cell.value if curr_cell else "")  # Append value or empty string if cell is blank
      
      # Print the extracted row as a list
      print("Row to List:", row_list)

      輸出:

      Row to List: ['City', 'Region', 'Store']

      使用 Python 將 Excel 列轉換為列表

      您還可以將單列提取到列表中。例如,我們將“Region”列轉換為列表:

      1. 加載工作簿和工作表。
      2. 通過索引選擇列。
      3. 遍歷列中的每一行。
      4. 將列值收集到列表中。
      # Import Aspose.Cells library
      from aspose.cells import Workbook
      
      # Step 1: Load the Excel workbook from file
      book = Workbook("sample_data.xlsx")
      
      # Access the first worksheet in the workbook
      sheet = book.worksheets.get(0)
      
      # Step 2: Define the column index (0 = first column, i.e., Column A)
      col_index = 0
      cells = sheet.cells
      
      # Create a range object for the selected column
      # Parameters: (start_row, start_column, total_rows, total_columns)
      # Here, start at row 0, select col_index, include all rows, and width = 1 column
      col_range = cells.create_range(0, col_index, sheet.cells.max_row + 1, 1)
      
      # Step 3 & 4: Convert the column into a Python list
      col_list = []
      for row_index in range(col_range.first_row, col_range.first_row + col_range.row_count):
          curr_cell = cells.check_cell(row_index, col_index)  # Get each cell in the column
          if curr_cell:  # Only add if the cell exists (ignore empty rows)
              col_list.append(curr_cell.value)
      
      # Print the extracted column as a list
      print("Column to List:", col_list)

      輸出:

      Column to List: ['City', 'Chicago', 'New York', 'Detroit']

      結論

      我們演示了如何通過 .NET 使用 Aspose.Cells for Python 提取范圍、行和列,將 Excel 數據轉換為 Python 列表。轉換為列表后,數據可用于 Pandas、JSON 或其他處理任務。雖然 openpyxl 或 pandas.read_excel 等庫可以提取范圍,但 Aspose.Cells 能夠更好地控制公式、格式、圖表和合并單元格,使其成為復雜 Excel 操作的更佳選擇。

      Aspose.Cells官方試用版免費下載,請聯系Aspose官方授權代理商慧都科技

      加入Aspose技術交流QQ群(1041253375),與更多小伙伴一起探討提升開發技能。

      posted @ 2025-09-17 11:57  IT開發者筆記  閱讀(16)  評論(0)    收藏  舉報
      主站蜘蛛池模板: 亚洲精品综合一区二区在线 | 电影在线观看+伦理片| 亚洲一区二区三区在线观看精品中文| 精品国产第一国产综合精品| 成人午夜电影福利免费| 日本高清在线观看WWW色| 国产三级黄色片在线观看| 大尺度国产一区二区视频 | 色综合久久久久综合体桃花网| 国产毛片精品av一区二区| 国产精品原创不卡在线| 亚洲精品久久久久久无码色欲四季 | 男女扒开双腿猛进入爽爽免费看| 国产亚洲另类无码专区| 日韩在线视频网| 中文无码精品a∨在线| 欧洲精品色在线观看| 午夜DY888国产精品影院| 欧美日本一区二区视频在线观看 | 久久综合亚洲鲁鲁九月天| 国产目拍亚洲精品二区| 蜜臀91精品高清国产福利| 91网站在线看| 无码人妻少妇色欲av一区二区 | 性一交一黄一片| 日韩视频一区二区三区视频| 麻花传剧mv在线看免费| 日本精品不卡一二三区| 蜜臀av一区二区三区日韩| 无码人妻aⅴ一区二区三区69岛| 日韩一区二区黄色一级片| 亚洲中文字幕无码爆乳APP| 欧美野外伦姧在线观看| 日韩精品一区二区高清视频| 亚洲国产制服丝袜先锋| 亚洲精品国产精品国在线| 欧美白人最猛性xxxxx| 天天影视色香欲综合久久| 亚洲天堂精品一区二区| 五十路丰满中年熟女中出| 国产SM重味一区二区三区|