使用 openpyxl 求和单元格值

问题描述 投票:0回答:1

`我有名为“f1-results.xlsx”的 Excel 工作表文件,其中包含按种族(国家名称)命名的选项卡:澳大利亚、中国、巴林、俄罗斯等。每个选项卡都包含以下格式的排名:

1 5 塞巴斯蒂安·维特尔 VET 法拉利 1:24:11.672 25

第一列 (A) 是位置(比赛后的位置),下一列 (B) 不相关,另一列 (C) 包含车手姓名(我需要它),F 列包含在本场比赛中得分(我也需要)。看起来像这样:
表(种族)“澳大利亚”:

1   5   Sebastian Vettel VET    Ferrari 1:24:11.672 25
2   44  Lewis Hamilton HAM  Mercedes    +9.975s 18
3   77  Valtteri  Bottas BOT    Mercedes    +11.250s    15
4   7   Kimi Räikkönen RAI  Ferrari +22.393s    12
5   33  Max Verstappen VER  Red Bull Racing TAG Heuer   +28.827s    10

表(种族)“中国”:

1   44  Lewis Hamilton HAM  Mercedes    1:37:36.158 25
2   5   Sebastian Vettel VET    Ferrari +6.250s 18
3   33  Max Verstappen VER  Red Bull Racing TAG Heuer   +45.192s    15
4   3   Daniel Ricciardo RIC    Red Bull Racing TAG Heuer   +46.035s    12
5   7   Kimi Räikkönen RAI  Ferrari +48.076s    10
6   77  Valtteri  Bottas BOT    Mercedes    +48.808s    8

请记住,每场比赛中车手的排列顺序都不同。

基于此文件,我必须制作一个新的 xlsx 文件,其中包含名为“Standings”的表格,该表格将显示每位车手每次比赛后的积分数。

例如:
澳大利亚 中国
刘易斯·汉密尔顿 18 43
(因为他在澳洲站拿了18分,在中国站拿了25分,所以18+25)

这是我的代码,但我无法在每场比赛后总结分数。
现在显示如下:

Australia   China   
Lewis Hamilton  18  25
from openpyxl import load_workbook, Workbook

# Load the workbook
wb = load_workbook(filename='/kaggle/input/f1-results/f1-results.xlsx')

# Create a new workbook for standings
standings_workbook = Workbook()
standings_sheet = standings_workbook.active
standings_sheet.title = 'Standings'

# Initialize a dictionary to store points for each driver
driver_points = {}

# Iterate through each sheet (race)
for sheet_name in wb.sheetnames:
    # Get the current sheet
    race_sheet = wb[sheet_name]
    
    # Iterate through rows in the current sheet starting from 2nd row (excluding headers)
    for row in range(2, race_sheet.max_row + 1):
        # Extract driver name and points
        driver_name = race_sheet.cell(row=row, column=3).value
        points = race_sheet.cell(row=row, column=6).value
        
        # Update points for the driver
        if driver_name in driver_points:
            driver_points[driver_name][sheet_name] = points
        else:
            driver_points[driver_name] = {sheet_name: points}

# Write headers for each race
race_index = 2
for sheet_name in wb.sheetnames:
    standings_sheet.cell(row=1, column=race_index, value=sheet_name)
    race_index += 1

# Write driver names and points after each race
driver_index = 2
for driver, race_points in driver_points.items():
    standings_sheet.cell(row=driver_index, column=1, value=driver)
    
    race_index = 2
    for sheet_name in wb.sheetnames:
        points = race_points.get(sheet_name, 0)  # Get points for this race, or default to 0 if not found
        standings_sheet.cell(row=driver_index, column=race_index, value=points)
        race_index += 1
    
    driver_index += 1

# Sum up the points for each driver
for row in range(2, standings_sheet.max_row + 1):
    total_points = sum(int(standings_sheet.cell(row=row, column=col).value or 0) for col in range(2, standings_sheet.max_column + 1))
    standings_sheet.cell(row=row, column=standings_sheet.max_column + 1, value=total_points)

# Save the standings workbook
standings_workbook.save(filename='_driver_standings.xlsx')

我不知道如何让它将以下单元格相互添加。

python openpyxl
1个回答
0
投票

是的,它似乎确实在求和,
但是当你写下总数时

standings_sheet.cell(row=row, column=standings_sheet.max_column + 1, value=total_points)

每次写入都会移动一列。

standings_sheet.max_column
是每次读取时实时的最后一列。

例如,对于第一个司机,您用

standings_sheet.max_column
= 10 写入总和,因此总计写入第 11 列(K 列) 最后一列或
standings_sheet.max_column
现在等于 11。 因此,在下一个驱动程序中,最后一列为 11 意味着将写入的列为 12,对于所有驱动程序,依此类推。

您想在循环之前读取“max_column”,添加 1,然后对所有驱动程序使用该值。喜欢;

totals_column = standings_sheet.max_column + 1
for row in range(2, standings_sheet.max_row + 1):
    total_points = sum(int(standings_sheet.cell(row=row, column=col).value or 0) for col in range(2, standings_sheet.max_column + 1))
    standings_sheet.cell(row=row, column=totals_column, value=total_points)

© www.soinside.com 2019 - 2024. All rights reserved.