gpt4 book ai didi

python - 从文本文件的行中提取数据

转载 作者:太空狗 更新时间:2023-10-29 22:03:43 25 4
gpt4 key购买 nike

我需要从文本文件的行中提取数据。数据是名称和评分信息,格式如下:

Shyvana - 12/4/5 - Loss - 2012-11-22
Fizz - 12/4/5 - Win - 2012-11-22
Miss Fortune - 12/4/3 - Win - 2012-11-22

此文件由我的小 Python 程序的另一部分生成,我在其中询问用户姓名,查找他们输入的姓名以确保其从姓名列表中有效,然后询问击杀、死亡、助攻和无论他们赢了还是输了。然后我要求确认并将该数据写入新行的文件,并在最后附加日期。准备该数据的代码:

data = "%s - %s/%s/%s - %s - %s\n" % (
champname, kills, deaths, assists, winloss, timestamp)

基本上,我想在程序的另一部分读回该数据并将其显示给用户,并用它进行计算,就像特定名称随时间推移的平均值一样。

我是 python 的新手,而且我在一般编程方面不是很有经验,所以我发现的大多数字符串拆分和格式化示例都太神秘了,我无法理解如何适应我在这里需要的东西,谁能帮忙?我可以以不同的方式格式化写入的数据,这样标记查找会更简单,但我希望它直接在文件中变得简单。

最佳答案

以下内容会将所有内容读入以玩家名称为关键字的字典中。与每个玩家关联的值本身就是一个字典,充当记录,其中包含与转换为适合进一步处理的格式的项目关联的命名字段。

info = {}
with open('scoring_info.txt') as input_file:
for line in input_file:
player, stats, outcome, date = (
item.strip() for item in line.split('-', 3))
stats = dict(zip(('kills', 'deaths', 'assists'),
map(int, stats.split('/'))))
date = tuple(map(int, date.split('-')))
info[player] = dict(zip(('stats', 'outcome', 'date'),
(stats, outcome, date)))

print('info:')
for player, record in info.items():
print(' player %r:' % player)
for field, value in record.items():
print(' %s: %s' % (field, value))

# sample usage
player = 'Fizz'
print('\n%s had %s kills in the game' % (player, info[player]['stats']['kills']))

输出:

info:
player 'Shyvana':
date: (2012, 11, 22)
outcome: Loss
stats: {'assists': 5, 'kills': 12, 'deaths': 4}
player 'Miss Fortune':
date: (2012, 11, 22)
outcome: Win
stats: {'assists': 3, 'kills': 12, 'deaths': 4}
player 'Fizz':
date: (2012, 11, 22)
outcome: Win
stats: {'assists': 5, 'kills': 12, 'deaths': 4}

Fizz had 12 kills in the game

或者,与其将大部分数据保存在字典中,这会使嵌套字段访问有点尴尬 — info[player]['stats']['kills'] — 您可以而是使用更高级的“通用”类来保存它们,这样您就可以编写 info2[player].stats.kills

为了说明这一点,这里使用我命名为 Struct 的类几乎是相同的事情,因为它有点像 C 语言的 struct 数据类型:

class Struct(object):
""" Generic container object """
def __init__(self, **kwds): # keyword args define attribute names and values
self.__dict__.update(**kwds)

info2 = {}
with open('scoring_info.txt') as input_file:
for line in input_file:
player, stats, outcome, date = (
item.strip() for item in line.split('-', 3))
stats = dict(zip(('kills', 'deaths', 'assists'),
map(int, stats.split('/'))))
victory = (outcome.lower() == 'win') # change to boolean T/F
date = dict(zip(('year','month','day'), map(int, date.split('-'))))
info2[player] = Struct(champ_name=player, stats=Struct(**stats),
victory=victory, date=Struct(**date))
print('info2:')
for rec in info2.values():
print(' player %r:' % rec.champ_name)
print(' stats: kills=%s, deaths=%s, assists=%s' % (
rec.stats.kills, rec.stats.deaths, rec.stats.assists))
print(' victorious: %s' % rec.victory)
print(' date: %d-%02d-%02d' % (rec.date.year, rec.date.month, rec.date.day))

# sample usage
player = 'Fizz'
print('\n%s had %s kills in the game' % (player, info2[player].stats.kills))

输出:

info2:
player 'Shyvana':
stats: kills=12, deaths=4, assists=5
victorious: False
date: 2012-11-22
player 'Miss Fortune':
stats: kills=12, deaths=4, assists=3
victorious: True
date: 2012-11-22
player 'Fizz':
stats: kills=12, deaths=4, assists=5
victorious: True
date: 2012-11-22

Fizz had 12 kills in the game

关于python - 从文本文件的行中提取数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13521397/

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