gpt4 book ai didi

python - 搜索文本文件 - 高分(使用 Python)

转载 作者:太空宇宙 更新时间:2023-11-04 08:36:40 25 4
gpt4 key购买 nike

这是我的代码:

results = [[username, score]]

if (topic == 'history') and (difficulty == 'easy'):
with open("hisEasyR.txt", "a") as hisEasyRFile:
writer = csv.writer(hisEasyRFile, delimiter='|')
writer.writerows(results)

这是文本文件的样子:

mary|4
john|5
lisa|3

我想从文件中读取这个文件并找到最高分。也许使用 max 函数?不过,我不确定该怎么做。有没有一种方法可以像搜索列表一样搜索文件?

最佳答案

Pandas 为此类任务提供了一个干净的界面:

import pandas as pd

df = pd.read_csv(filepath, delimiter='|', header=None, names=['username', 'score'])

highest_score = df['score'].max()
highest_score_username = df.loc[df['score'] == highest_score, 'username']

关于python - 搜索文本文件 - 高分(使用 Python),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48362221/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com