CSV,搜索字符串,获取正确的信息,以正确的格式编写正确的信息

时间:2013-03-21 23:51:42

标签: python string csv

我对堆栈溢出比较新,所以请耐心等待。另外,我为标题和任何含糊不清等道歉。

所以,这就是我要做的事情:

我正在使用.CSV格式的原始数据文件,其中包含播放 整个赛季的NBA比赛。

基于游戏的代码使用适当的代码创建一个新文件 游戏的标题。然后,它读取每一行。

每行包含游戏ID,行号(不相关),时间 剩下的,以及'播放信息'。

我在下面列举了一个例子(例1)。

需要编写的信息,见下文(例2), 球场上有10名球员:

  • 播放时间
  • 期间
  • 期间剩余的时间
  • 事件类型
  • 如果有助手
  • 球员获得助攻
  • 如果有犯规
  • 被犯罪的人

等。

“玩家”标题指定初始游戏的对象 记录(即,如果一名球员制作了一名球员名字为'射门' 记录)。录制该剧的团队也是 写入。

示例1:

GameID, LineNumber, TimeRemaining,  Entry

20071030HOULAL, 1,  0:48:00,        Start of 1st Quarter
20071030HOULAL, 2,  0:48:00,        Jump Ball Brown vs Yao
20071030HOULAL, 3,  0:47:42,        [LAL] Walton Jump Shot: Missed
20071030HOULAL, 4,  0:47:41,        [LAL] Brown Rebound (Off:1 Def:0)
20071030HOULAL, 5,  0:47:29,        [LAL] Bryant Jump Shot: Missed
20071030HOULAL, 6,  0:47:28,        [HOU] Alston Rebound (Off:0 Def:1)
20071030HOULAL, 7,  0:47:06,        [HOU] Yao Jump Shot: Missed

示例2:

a1(away),a2(away), a3(away), a4, a5, h1, h2, h3, h4, h5, period,time,team, etype, assist, away, block, entered, home, left, num, opponent, outof, player, points,possession, reason, result, steal

(表示为csv文件中的每一行存储的值 - 试图包含样本数据太麻烦了)

问题在于:

对于每支球队,都有球员名单。我的代码获得了播放器 名单,然后只使用球员的姓氏创建一个名单, (这就是玩家姓名的记录方式。)

第一个问题出现在为每个球员累积所有10名球员时 球队,在球场上。

我使用累加器存储播放数据,直到所有10个玩家都是 录制,然后写入播放数据,添加10个播放器。

问题是有些玩家在检查时会被“跳过” 对指定为球场上的球员的变量进行了表现。

例如,有一个为Luther Head录制的剧本 休斯顿,但他永远不会被记录到a1, a2, a3, a4a5(当前比赛的客队)。

以下是设置“检查”的方法,firstPlayer为 录制的人和secondPlayer是一个人 其他玩家,例如助手,被记录为:

if team == homeTeam:
    if h1 == ' ' and firstPlayer != h2 and firstPlayer != h3 and firstPlayer != h4 and firstPlayer != h5:
        h1 = firstPlayer
    if h2 == ' ' and firstPlayer != h1 and firstPlayer != h3 and firstPlayer != h4 and firstPlayer != h5:
        h2 = firstPlayer
    if h3 == ' ' and firstPlayer != h1 and firstPlayer != h2 and firstPlayer != h4 and firstPlayer != h5:
        h3 = firstPlayer
    if h4 == ' ' and firstPlayer != h1 and firstPlayer != h2 and firstPlayer != h3 and firstPlayer != h5:
        h4 = firstPlayer
    if h5 == ' ' and firstPlayer != h1 and firstPlayer != h2 and firstPlayer != h3 and firstPlayer != h4:
        h5 = firstPlayer               

if team == visitingTeam:
    if a1 == ' ' and firstPlayer != a2 and firstPlayer != a3 and firstPlayer != a4 and firstPlayer != a5:
        a1 = firstPlayer
    if a2 == ' ' and firstPlayer != a1 and firstPlayer != a3 and firstPlayer != a4 and firstPlayer != a5:
        a2 = firstPlayer
    if a3 == ' ' and firstPlayer != a1 and firstPlayer != a2 and firstPlayer != a4 and firstPlayer != a5:
        a3 = firstPlayer
    if a4 == ' ' and firstPlayer != a1 and firstPlayer != a2 and firstPlayer != a3 and firstPlayer != a5:
        a4 = firstPlayer
    if a5 == ' ' and firstPlayer != a1 and firstPlayer != a2 and firstPlayer != a3 and firstPlayer != a4:
        a5 = firstPlayer

if etype != 'sub':  <<<<----This type of event is diffent
    if team == homeTeam and secondPlayer != ' ':
        if h1 == ' ' and secondPlayer != h2 and secondPlayer != h3 and secondPlayer != h4 and secondPlayer != h5:
            h1 = secondPlayer
        if h2 == ' ' and secondPlayer != h1 and secondPlayer != h3 and secondPlayer != h4 and secondPlayer != h5:
            h2 = secondPlayer 
        if h3 == ' ' and secondPlayer != h1 and secondPlayer != h2 and secondPlayer != h4 and secondPlayer != h5:
            h3 = secondPlayer 
        if h4 == ' ' and secondPlayer != h1 and secondPlayer != h2 and secondPlayer != h3 and secondPlayer != h5:
            h4 = secondPlayer 
        if h5 == ' ' and secondPlayer != h1 and secondPlayer  != h2 and secondPlayer != h3 and secondPlayer != h4:
            h5 = secondPlayer

    if team == visitingTeam and secondPlayer != ' ':
        if a1 == ' ' and secondPlayer != a2 and secondPlayer != a3 and secondPlayer != a4 and secondPlayer != a5:
            a1 = secondPlayer
        if a2 == ' ' and secondPlayer != a1 and secondPlayer != a3 and secondPlayer != a4 and secondPlayer != a5:
            a2 = secondPlayer
        if a3 == ' ' and secondPlayer != a1 and secondPlayer != a2 and secondPlayer != a4 and secondPlayer != a5:
            a3 = secondPlayer
        if a4 == ' ' and secondPlayer != a1 and secondPlayer != a2 and secondPlayer != a3 and secondPlayer != a5:
            a4 = secondPlayer
        if a5 == ' ' and secondPlayer != a1 and secondPlayer != a2 and secondPlayer != a3 and secondPlayer != a4:
            a5 = secondPlayer

我想这确实是我遇到的唯一问题。

虽然,我想找到一种更好的方法来搜索字符串和 获取信息。

为了获得玩家,我将字符串的前半部分分配给一个 变量和下半年到另一半。然后我拆分字符串(sep = ' '),所以它在列表中。

然后我检查每个字符串以查看是否有单词 是==给名单中的任何一名球员,具体取决于哪支球队 该剧被录制了。

如果是第一个字符串,该播放器会被分配firstPlayer 变量,如果是第二个,那个玩家被分配了 secondPlayer变量。

当我搜索etype(事件类型)时,我只是用以下表达式搜索字符串:

if 'Shot' in string:
   etype = 'shot'
   player = firstPlayer
   if 'Assist' in string:
       assist = secondPlayer

......等等各种事件类型。

如果您需要其他信息来帮助回答此问题,请与我们联系。

0 个答案:

没有答案