Hướng dẫn how do i read a specific row in excel using python? - làm cách nào để đọc một hàng cụ thể trong excel bằng python?

Tôi muốn đọc một hàng duy nhất từ ​​excel_file1, sheet1, hàng số 7 bằng Python, có trợ giúp nào không?

Đã hỏi ngày 23 tháng 1 năm 2019 lúc 9:04Jan 23, 2019 at 9:04

Cài đặt XLRD đầu tiên

pip install xlrd

sau đó mở tệp python và

import xlrd 

# Give the location of the file 
loc = ["path of file"] 

# To open Workbook 
wb = xlrd.open_workbook[loc] 
sheet = wb.sheet_by_index[0] 

print[sheet.row_values[7]] 

Vị trí là đường dẫn tương đối không phải là đường dẫn tuyệt đối.

Để đọc thêm về XLRD và việc sử dụng của nó, hãy truy cập //xlrd.readthedocs.io/en/latest/

Mã hóa hạnh phúc.

Đã trả lời ngày 23 tháng 1 năm 2019 lúc 9:11Jan 23, 2019 at 9:11

anand_v.singhanand_v.singhanand_v.singh

2.6501 Huy hiệu vàng15 Huy hiệu bạc34 Huy hiệu đồng1 gold badge15 silver badges34 bronze badges

0

Bạn cũng có thể sử dụng thư viện pd.read_excel của pandas:

Bạn sẽ cần cài đặt pandasxlrd trước:

import pandas as pd
import xlrd
df = pd.read_excel['abc.xlsx', sheet_name='Sheet1']

Bây giờ, bạn có thể lọc DataFrame của mình để nhận bất kỳ hàng cụ thể nào bằng cách sử dụng iloc

df.iloc[6] ## This will give you 7th row

Đã trả lời ngày 23 tháng 1 năm 2019 lúc 9:17Jan 23, 2019 at 9:17

Mayank porwalmayank porwalMayank Porwal

32.6K8 Huy hiệu vàng32 Huy hiệu bạc55 Huy hiệu Đồng8 gold badges32 silver badges55 bronze badges

0

Xem thảo luận

Cải thiện bài viết

Lưu bài viết

  • Đọc
  • Bàn luận
  • Xem thảo luận

    Cải thiện bài viết

    Lưu bài viết

    ĐọcOpenpyxl module in python. Openpyxlis a Python library for reading and writing Excel [with extension xlsx/xlsm/xltx/xltm] files. The openpyxl module allows a Python program to read and modify Excel files.

    Bàn luận

    Trong bài viết này, chúng ta sẽ thảo luận về cách lặp lại thông qua các hàng excel trong Python. Để thực hiện nhiệm vụ này, chúng tôi sẽ sử dụng mô -đun OpenPyXL trong Python. OpenPyXLIS Một thư viện Python để đọc và ghi các tệp Excel [với phần mở rộng XLSX/XLSM/XLTX/XLTM]. Mô -đun OpenPyXL cho phép chương trình Python đọc và sửa đổi các tệp Excel.

    Chúng tôi sẽ sử dụng bảng tính Excel này trong các ví dụ dưới đây:

    Python3

    Cách tiếp cận số 1:

    Chúng tôi sẽ tạo một đối tượng của OpenPyXL, và sau đó chúng tôi sẽ lặp qua tất cả các hàng từ trên xuống dưới.

    import

    import xlrd 
    
    # Give the location of the file 
    loc = ["path of file"] 
    
    # To open Workbook 
    wb = xlrd.open_workbook[loc] 
    sheet = wb.sheet_by_index[0] 
    
    print[sheet.row_values[7]] 
    
    0

    import xlrd 
    
    # Give the location of the file 
    loc = ["path of file"] 
    
    # To open Workbook 
    wb = xlrd.open_workbook[loc] 
    sheet = wb.sheet_by_index[0] 
    
    print[sheet.row_values[7]] 
    
    1
    import xlrd 
    
    # Give the location of the file 
    loc = ["path of file"] 
    
    # To open Workbook 
    wb = xlrd.open_workbook[loc] 
    sheet = wb.sheet_by_index[0] 
    
    print[sheet.row_values[7]] 
    
    2
    import xlrd 
    
    # Give the location of the file 
    loc = ["path of file"] 
    
    # To open Workbook 
    wb = xlrd.open_workbook[loc] 
    sheet = wb.sheet_by_index[0] 
    
    print[sheet.row_values[7]] 
    
    3
    import xlrd 
    
    # Give the location of the file 
    loc = ["path of file"] 
    
    # To open Workbook 
    wb = xlrd.open_workbook[loc] 
    sheet = wb.sheet_by_index[0] 
    
    print[sheet.row_values[7]] 
    
    4
    import xlrd 
    
    # Give the location of the file 
    loc = ["path of file"] 
    
    # To open Workbook 
    wb = xlrd.open_workbook[loc] 
    sheet = wb.sheet_by_index[0] 
    
    print[sheet.row_values[7]] 
    
    5

    import pandas as pd
    import xlrd
    df = pd.read_excel['abc.xlsx', sheet_name='Sheet1']
    
    9
    df.iloc[6] ## This will give you 7th row
    
    0
    import pandas as pd
    import xlrd
    df = pd.read_excel['abc.xlsx', sheet_name='Sheet1']
    
    3
    df.iloc[6] ## This will give you 7th row
    
    2
    import xlrd 
    
    # Give the location of the file 
    loc = ["path of file"] 
    
    # To open Workbook 
    wb = xlrd.open_workbook[loc] 
    sheet = wb.sheet_by_index[0] 
    
    print[sheet.row_values[7]] 
    
    5

    import pandas as pd
    import xlrd
    df = pd.read_excel['abc.xlsx', sheet_name='Sheet1']
    
    9
    df.iloc[6] ## This will give you 7th row
    
    0
    import pandas as pd
    import xlrd
    df = pd.read_excel['abc.xlsx', sheet_name='Sheet1']
    
    3
    df.iloc[6] ## This will give you 7th row
    
    7
    df.iloc[6] ## This will give you 7th row
    
    8
    df.iloc[6] ## This will give you 7th row
    
    9
    import xlrd 
    
    # Give the location of the file 
    loc = ["path of file"] 
    
    # To open Workbook 
    wb = xlrd.open_workbook[loc] 
    sheet = wb.sheet_by_index[0] 
    
    print[sheet.row_values[7]] 
    
    5

    import xlrd 
    
    # Give the location of the file 
    loc = ["path of file"] 
    
    # To open Workbook 
    wb = xlrd.open_workbook[loc] 
    sheet = wb.sheet_by_index[0] 
    
    print[sheet.row_values[7]] 
    
    6
    import xlrd 
    
    # Give the location of the file 
    loc = ["path of file"] 
    
    # To open Workbook 
    wb = xlrd.open_workbook[loc] 
    sheet = wb.sheet_by_index[0] 
    
    print[sheet.row_values[7]] 
    
    2
    import xlrd 
    
    # Give the location of the file 
    loc = ["path of file"] 
    
    # To open Workbook 
    wb = xlrd.open_workbook[loc] 
    sheet = wb.sheet_by_index[0] 
    
    print[sheet.row_values[7]] 
    
    8

    import xlrd 
    
    # Give the location of the file 
    loc = ["path of file"] 
    
    # To open Workbook 
    wb = xlrd.open_workbook[loc] 
    sheet = wb.sheet_by_index[0] 
    
    print[sheet.row_values[7]] 
    
    9
    import pandas as pd
    import xlrd
    df = pd.read_excel['abc.xlsx', sheet_name='Sheet1']
    
    0
    import pandas as pd
    import xlrd
    df = pd.read_excel['abc.xlsx', sheet_name='Sheet1']
    
    1
    import pandas as pd
    import xlrd
    df = pd.read_excel['abc.xlsx', sheet_name='Sheet1']
    
    2
    import pandas as pd
    import xlrd
    df = pd.read_excel['abc.xlsx', sheet_name='Sheet1']
    
    3
    import pandas as pd
    import xlrd
    df = pd.read_excel['abc.xlsx', sheet_name='Sheet1']
    
    4
    import pandas as pd
    import xlrd
    df = pd.read_excel['abc.xlsx', sheet_name='Sheet1']
    
    5
    import pandas as pd
    import xlrd
    df = pd.read_excel['abc.xlsx', sheet_name='Sheet1']
    
    6
    import pandas as pd
    import xlrd
    df = pd.read_excel['abc.xlsx', sheet_name='Sheet1']
    
    4
    import pandas as pd
    import xlrd
    df = pd.read_excel['abc.xlsx', sheet_name='Sheet1']
    
    8

    pandas2

    df.iloc[6] ## This will give you 7th row
    
    0pandas2
    import xlrd 
    
    # Give the location of the file 
    loc = ["path of file"] 
    
    # To open Workbook 
    wb = xlrd.open_workbook[loc] 
    sheet = wb.sheet_by_index[0] 
    
    print[sheet.row_values[7]] 
    
    2pandas4
    import xlrd 
    
    # Give the location of the file 
    loc = ["path of file"] 
    
    # To open Workbook 
    wb = xlrd.open_workbook[loc] 
    sheet = wb.sheet_by_index[0] 
    
    print[sheet.row_values[7]] 
    
    5

    Output:

    import pandas as pd
    import xlrd
    df = pd.read_excel['abc.xlsx', sheet_name='Sheet1']
    
    9
    import xlrd 
    
    # Give the location of the file 
    loc = ["path of file"] 
    
    # To open Workbook 
    wb = xlrd.open_workbook[loc] 
    sheet = wb.sheet_by_index[0] 
    
    print[sheet.row_values[7]] 
    
    9 pd.read_excel3
    import pandas as pd
    import xlrd
    df = pd.read_excel['abc.xlsx', sheet_name='Sheet1']
    
    1
    import pandas as pd
    import xlrd
    df = pd.read_excel['abc.xlsx', sheet_name='Sheet1']
    
    2
    import pandas as pd
    import xlrd
    df = pd.read_excel['abc.xlsx', sheet_name='Sheet1']
    
    3
    import pandas as pd
    import xlrd
    df = pd.read_excel['abc.xlsx', sheet_name='Sheet1']
    
    4pd.read_excel8
    import pandas as pd
    import xlrd
    df = pd.read_excel['abc.xlsx', sheet_name='Sheet1']
    
    6
    import pandas as pd
    import xlrd
    df = pd.read_excel['abc.xlsx', sheet_name='Sheet1']
    
    4
    import pandas as pd
    import xlrd
    df = pd.read_excel['abc.xlsx', sheet_name='Sheet1']
    
    8

    pandas2pandas3

    import xlrd 
    
    # Give the location of the file 
    loc = ["path of file"] 
    
    # To open Workbook 
    wb = xlrd.open_workbook[loc] 
    sheet = wb.sheet_by_index[0] 
    
    print[sheet.row_values[7]] 
    
    2 pandas5
    import xlrd 
    
    # Give the location of the file 
    loc = ["path of file"] 
    
    # To open Workbook 
    wb = xlrd.open_workbook[loc] 
    sheet = wb.sheet_by_index[0] 
    
    print[sheet.row_values[7]] 
    
    2pandas7__12

    Python3

    Cách tiếp cận số 1:

    Chúng tôi sẽ tạo một đối tượng của OpenPyXL, và sau đó chúng tôi sẽ lặp qua tất cả các hàng từ trên xuống dưới.

    import

    import xlrd 
    
    # Give the location of the file 
    loc = ["path of file"] 
    
    # To open Workbook 
    wb = xlrd.open_workbook[loc] 
    sheet = wb.sheet_by_index[0] 
    
    print[sheet.row_values[7]] 
    
    0

    import xlrd 
    
    # Give the location of the file 
    loc = ["path of file"] 
    
    # To open Workbook 
    wb = xlrd.open_workbook[loc] 
    sheet = wb.sheet_by_index[0] 
    
    print[sheet.row_values[7]] 
    
    1
    import xlrd 
    
    # Give the location of the file 
    loc = ["path of file"] 
    
    # To open Workbook 
    wb = xlrd.open_workbook[loc] 
    sheet = wb.sheet_by_index[0] 
    
    print[sheet.row_values[7]] 
    
    2
    import xlrd 
    
    # Give the location of the file 
    loc = ["path of file"] 
    
    # To open Workbook 
    wb = xlrd.open_workbook[loc] 
    sheet = wb.sheet_by_index[0] 
    
    print[sheet.row_values[7]] 
    
    3
    import xlrd 
    
    # Give the location of the file 
    loc = ["path of file"] 
    
    # To open Workbook 
    wb = xlrd.open_workbook[loc] 
    sheet = wb.sheet_by_index[0] 
    
    print[sheet.row_values[7]] 
    
    4
    import xlrd 
    
    # Give the location of the file 
    loc = ["path of file"] 
    
    # To open Workbook 
    wb = xlrd.open_workbook[loc] 
    sheet = wb.sheet_by_index[0] 
    
    print[sheet.row_values[7]] 
    
    5

    import xlrd 
    
    # Give the location of the file 
    loc = ["path of file"] 
    
    # To open Workbook 
    wb = xlrd.open_workbook[loc] 
    sheet = wb.sheet_by_index[0] 
    
    print[sheet.row_values[7]] 
    
    6
    import xlrd 
    
    # Give the location of the file 
    loc = ["path of file"] 
    
    # To open Workbook 
    wb = xlrd.open_workbook[loc] 
    sheet = wb.sheet_by_index[0] 
    
    print[sheet.row_values[7]] 
    
    2
    import xlrd 
    
    # Give the location of the file 
    loc = ["path of file"] 
    
    # To open Workbook 
    wb = xlrd.open_workbook[loc] 
    sheet = wb.sheet_by_index[0] 
    
    print[sheet.row_values[7]] 
    
    8

    pandas2

    df.iloc[6] ## This will give you 7th row
    
    0import9
    import xlrd 
    
    # Give the location of the file 
    loc = ["path of file"] 
    
    # To open Workbook 
    wb = xlrd.open_workbook[loc] 
    sheet = wb.sheet_by_index[0] 
    
    print[sheet.row_values[7]] 
    
    2pandas4
    import xlrd 
    
    # Give the location of the file 
    loc = ["path of file"] 
    
    # To open Workbook 
    wb = xlrd.open_workbook[loc] 
    sheet = wb.sheet_by_index[0] 
    
    print[sheet.row_values[7]] 
    
    5

    import pandas as pd
    import xlrd
    df = pd.read_excel['abc.xlsx', sheet_name='Sheet1']
    
    9
    df.iloc[6] ## This will give you 7th row
    
    0
    import xlrd 
    
    # Give the location of the file 
    loc = ["path of file"] 
    
    # To open Workbook 
    wb = xlrd.open_workbook[loc] 
    sheet = wb.sheet_by_index[0] 
    
    print[sheet.row_values[7]] 
    
    05

    Output:


    Làm cách nào để đọc một hàng cụ thể trong Excel trong Python?

    Sử dụng hàm pandas.read_excel [] để đọc bảng excel vào gấu trúc DataFrame, theo mặc định, nó tải tờ đầu tiên từ tệp excel và phân tích hàng đầu tiên dưới dạng tên cột DataFrame. read_excel[] function to read excel sheet into pandas DataFrame, by default it loads the first sheet from the excel file and parses the first row as a DataFrame column name.

    Làm cách nào để trích xuất các hàng cụ thể trong Excel?

    Trích xuất các hàng đáp ứng các tiêu chí với kutools cho Excel..
    Chọn cột bạn trích xuất các hàng dựa trên và nhấp vào Kutools> Chọn> Chọn các ô cụ thể ..
    Sau đó, trong hộp thoại Chọn các ô cụ thể, kiểm tra toàn bộ tùy chọn hàng trong loại lựa chọn và chỉ định tiêu chí của bạn trong phần Chỉ định loại ..

    Làm cách nào để đọc các hàng cụ thể trong gấu trúc?

    Làm cách nào để đọc một hàng cụ thể trong gấu trúc ?..
    Bước 1: Thiết lập dữ liệu ..
    Bước 2: Nhập dữ liệu CSV ..
    Bước 3: Chọn hàng từ Pandas DataFrame ..
    Chọn các hàng gấu trúc bằng thuộc tính ILOC ..
    Chọn các hàng gấu trúc bằng thuộc tính LỘC ..

    Làm cách nào để đọc một cột cụ thể trong Excel trong Python?

    Làm cách nào để trích xuất các cột cụ thể trong Excel bằng Python ?..
    Nhập Gandas dưới dạng PD ..
    Nhập Numpy dưới dạng NP ..
    file_loc = "path.xlsx".
    df = pd.read_excel [file_loc, index_col = none, na_values = ['na'], usecols = "a, c: aa"].
    print[df].

    Chủ Đề