我是Python新手。基本上,我想编写一个程序来从 Excel 文件中读取列D& E,并计算总数Incoming和Outgoing持续时间。
哪个Python模块用于读取excel文件以及如何处理其中的数据?
Excel 文件:
D E
Incoming 18
Outgoing 99
Incoming 20
Outgoing 59
Incoming 30
Incoming 40
Run Code Online (Sandbox Code Playgroud)
根据您使用的 Excel 版本,有几个选项。
openpyxl - 用于读取 Excel 2010 文件(即:.xlsx)
xlrd - 用于读取较旧的 Excel 文件(即:.xls)
我只使用了 xlrd,你可以执行如下所示的操作
** 注意 ** 代码未经过测试
import xlrd
current_row = 0
sheet_num = 1
input_total = 0
output_total = 0
# path to the file you want to extract data from
src = r'c:\temp\excel sheet.xls'
book = xlrd.open_workbook(src)
# select the sheet where the data resides
work_sheet = book.sheet_by_index(sheet_num)
# get the total number of rows
num_rows = work_sheet.nrows - 1
while current_row < num_rows:
row_header = work_sheet.cell_value(current_row, 4)
if row_header == 'output':
output_total += work_sheet.cell_value(current_row, 5)
elif row_header == 'input':
input_total += work_sheet.cell_value(current_row, 5)
print output_total
print input_total
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
26376 次 |
| 最近记录: |