从文本文件的行中提取数据 [英] Extract data from lines of a text file

查看:253
本文介绍了从文本文件的行中提取数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要从文本文件的行中提取数据。数据是姓名和得分信息格式如下:
$ b

 =lang-none prettyprint-override>  Shyvana  -  12/4/5  - 损失 -  2012-11-22 
Fizz - 12/4/5 - 赢 - 2012-11-22
财富小姐 - 12/4/3 - 赢 - 2012-11-22

这个文件是由我的小python程序的另一部分生成的,我要求用户输入名字,输入以确保从名单中有效,然后要求杀人,死亡,助攻,以及他们是赢或输。然后,我要求确认,并将这些数据写入到新文件中,并在最后加上日期。准备数据的代码:

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



<基本上我想把这些数据读回到程序的另一部分,然后把它显示给用户,并对它进行计算,就像对一个特定的名字进行平均计算一样。



我是python的新手,而且我对编程一般也不是很熟练,所以我发现大多数字符串分割和格式化的例子都太神秘了,以至于我无法理解如何去适应我所需要的东西。帮帮我?我可以以不同的方式格式化写入的数据,所以标记的查找会更简单,但是我希望它能够直接在文件中直接使用。

解决方案

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

  info = {} 
with open('scoring_info.txt')as input_file:
for input_file中的行:
player,stats,outcome,date =(
item.big(' - ',3))
stats = dict((''杀死','死亡','助攻'),
map(int ,stats.split('/'))))
date = tuple(map(int,date.split(' - ')))
info [player] = dict(zip(('stats ','结果','日期'),
(统计数据,结果,日期)))

print('info:')
给玩家, items():
print('player%r:'%player)
for field,record.items()中的值:
print('%s:%s'% ,值))

#示例用法
play呃='Fizz'
print('\\\
%s有%s在游戏中被杀死%(player,info [player] ['stats'] ['kills']))

输出:
$ b

  info:
球员'Shyvana':
日期:(2012,11,22)
结果:损失
stats:{'assists':5 ,'kill':12,'deaths':4}
玩家'Fortune'小姐:
日期:(2012,11,22)
结果:Win
stats: '助攻':3,'杀死':12,'死亡':4}
玩家'Fizz':
日期:(2012,11,22)
结果:赢
统计:{'助攻':5,'杀死':12,'死亡':4}

Fizz在比赛中有12次杀死
pre>

或者,而不是将大部分数据保存在字典中,这可能会使嵌套字段访问有点尴尬 - info [player] [ 'stats'] ['kills'] - 您可以使用更高级的genericcla ss拿着他们,这会让你写 info2 [player] .stats.kills 来代替。



,使用一个名为 Struct 的类几乎是一样的,因为它类似于 struct 类型C:

 类结构(对象):
通用容器对象
def __init __(self,** kwds):#关键字args定义属性名称和值
self .__ dict __。update(** kwds)

info2 = {}
with open输入文件:
输入文件中的行:
player,stats,outcome,date =(
item.strip()for line.split(' - ' ,3))
stats = dict(('杀死','死亡','助攻'),
map(int,stats.split('/'))))
胜利=(outcome.lower()=='win')#转换为布尔值T / F
日期=字典(zip('year','month','day'),map(int, DA te.split(' - '))))
info2 [player] = Struct(champ_name = player,stats = Struct(** stats),
victory = victory,date = Struct(** date ))
print('info2:')
for info2.values():
print('player%r:'%rec.champ_name)
print('统计:杀人=%s,死亡=%s,助攻=%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))

#示例用法
player ='Fizz'
print('\\\
%s有%s杀死游戏'%(player,info2 [player] .stats.kills ))

输出:

  info2:
球员'Shyvana':
stats:kills = 12,deaths = 4,assists = 5
victorious:False
日期:2012-11-22
玩家'Miss Fortune':
stats :杀死= 12,死亡= 4,助攻= 3
胜利:真实
日期:2012-11-22
玩家嘶嘶声:
统计:杀死= 12,死亡= 4,助攻= 5
胜利:真
日期:2012-11-22

Fizz在比赛中有12次击杀


I need to extract data from lines of a text file. The data is name and scoring information formatted like this:

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

This file is generated by another part of my little python program where I ask the user for the name, lookup the name they enter to ensure it's valid from a list of names, and then ask for kills, deaths, assists, and whether they won or lost. Then I ask for confirmation and write that data to the file on a new line, and append the date at the end like that. The code that prepares that data:

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

Basically I want to read that data back in another part of the program and display it to the user and do calculations with it like averages over time for a particular name.

I'm new to python and and I'm not very experienced with programming in general so most of the string splitting and formatting examples I find are just too cryptic for me to understand how to adapt to quite what I need here, could anyone help? I could format the written data differently so token finding would be simpler, but I want it to be simple directly in the file.

解决方案

The following will read everything into a dictionary keyed by player name. The value associated with each player is itself a dictionary acting as a record with named fields associated with the items converted to a format suitable for further processing.

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']))

Output:

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

Alternatively, rather than holding most of the data in dictionaries, which can make nested-field access a little awkward — info[player]['stats']['kills'] — you could instead use a little more advanced "generic" class to hold them, which will let you write info2[player].stats.kills instead.

To illustrate, here's almost the same thing using a class I've named Struct because it's kind of like the struct type in the C:

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))

Output:

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

这篇关于从文本文件的行中提取数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆