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 pandas
và xlrd
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
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]]
0import 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]]
1import 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]]
3import 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]]
4import 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]]
5import pandas as pd
import xlrd
df = pd.read_excel['abc.xlsx', sheet_name='Sheet1']
9df.iloc[6] ## This will give you 7th row
0import pandas as pd
import xlrd
df = pd.read_excel['abc.xlsx', sheet_name='Sheet1']
3df.iloc[6] ## This will give you 7th row
2import 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]]
5import pandas as pd
import xlrd
df = pd.read_excel['abc.xlsx', sheet_name='Sheet1']
9df.iloc[6] ## This will give you 7th row
0import pandas as pd
import xlrd
df = pd.read_excel['abc.xlsx', sheet_name='Sheet1']
3df.iloc[6] ## This will give you 7th row
7df.iloc[6] ## This will give you 7th row
8df.iloc[6] ## This will give you 7th row
9import 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]]
5import 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]]
6import 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]]
8import 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']
0import 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']
2import pandas as pd
import xlrd
df = pd.read_excel['abc.xlsx', sheet_name='Sheet1']
3import pandas as pd
import xlrd
df = pd.read_excel['abc.xlsx', sheet_name='Sheet1']
4import pandas as pd
import xlrd
df = pd.read_excel['abc.xlsx', sheet_name='Sheet1']
5import pandas as pd
import xlrd
df = pd.read_excel['abc.xlsx', sheet_name='Sheet1']
6import pandas as pd
import xlrd
df = pd.read_excel['abc.xlsx', sheet_name='Sheet1']
4import pandas as pd
import xlrd
df = pd.read_excel['abc.xlsx', sheet_name='Sheet1']
8pandas
2
df.iloc[6] ## This will give you 7th row
0pandas
2import 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]]
2pandas
4import 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]]
5Output:
9import 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]]
pd.read_excel
3
1 import pandas as pd
import xlrd
df = pd.read_excel['abc.xlsx', sheet_name='Sheet1']
2import pandas as pd
import xlrd
df = pd.read_excel['abc.xlsx', sheet_name='Sheet1']
3import pandas as pd
import xlrd
df = pd.read_excel['abc.xlsx', sheet_name='Sheet1']
4import pandas as pd
import xlrd
df = pd.read_excel['abc.xlsx', sheet_name='Sheet1']
pd.read_excel
8
6import pandas as pd
import xlrd
df = pd.read_excel['abc.xlsx', sheet_name='Sheet1']
4import pandas as pd
import xlrd
df = pd.read_excel['abc.xlsx', sheet_name='Sheet1']
8import pandas as pd
import xlrd
df = pd.read_excel['abc.xlsx', sheet_name='Sheet1']
pandas
2pandas
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]]
2 pandas
5import 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]]
2pandas
7__12Python3
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]]
0import 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]]
1import 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]]
3import 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]]
4import 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]]
5import 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]]
6import 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]]
8pandas
2
df.iloc[6] ## This will give you 7th row
0import
9import 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]]
2pandas
4import 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]]
5import pandas as pd
import xlrd
df = pd.read_excel['abc.xlsx', sheet_name='Sheet1']
9df.iloc[6] ## This will give you 7th row
0import 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]]
05Output: