从Excel表中返回python中的几行



我想知道是否可以返回excel工作表的几行,其中一些列由一个唯一的字符串组成。然后我想将它们导出为CSV。

我在考虑openpyxl,但还没有走得太远。

如果我的Excel是这样的:

样品

例如,我会搜索ID2并返回所有行

ID2,1,ping
ID2,2,pong
from openpyxl import Workbook
import openpyxl
file = "test.xlsx"
wb = openpyxl.load_workbook(file, read_only=True)
ws = wb.active
for row in ws.iter_rows("A"):
for cell in row:
if cell.value == "ID2":
print(ws.cell(row=cell.row, column=1,2,3).value)

有人能帮我吗?

尝试使用pandapd.read_excel()pd.to_csv(),例如:

import pandas as pd
df = pd.read_excel('/file/path/excel.xslx')
df_filtered = df[df['id_column'] == 'ID1']  # returns df with only rows where 'id_column' is 'ID1'
df_filtered.to_csv('/file/path/output.csv')

将导出一个csv,其中只有'id_column'等于'ID1'的行。

最新更新