当信息不存在时,是否可以使用findall regex追加None或任何值

2024-06-26 10:56:53 发布

您现在位置:Python中文网/ 问答频道 /正文

我想解析文本并将其保存到dataframe。有时信息不存在,比如在二手车里,没有关于座位1的信息,我想在字典里追加一个或任何值。你知道吗

PokerStars Hand #163417399919: Hold'em No Limit (€1/€2 EUR) - 2016/12/23 23:48:52 CET [2016/12/23 17:48:52 ET]

Table 'Dnoces II' 6-max Seat #2 is the button
Seat 1: xxx ($200 in chips)
Seat 2: yyy ($364.58 in chips)
Seat 3: zxc ($200 in chips)
Seat 4: zdf ($235.43 in chips)
Seat 5: zdasdasII ($206.02 in chips)
Seat 6: assfds ($92.53 in chips)

PokerStars Hand #162960631727: Hold'em No Limit (€1/€2 EUR) - 2016/12/15 2:10:16 CET [2016/12/14 20:10:16 ET]

Table 'Suleika' 6-max Seat #4 is the button
Seat 2: xxx($137.08 in chips)
Seat 3: yyy ($200 in chips)
Seat 5: xyz($201.20 in chips)

PokerStars Hand #163416930846: Hold'em No Limit ($1/$2 USD) - 2016/12/23 23:39:57 CET [2016/12/23 17:39:57 ET]

Table 'Pieksamaki II' 6-max Seat #5 is the button
Seat 1: xxx (€230.90 in chips)
Seat 2: yyy (€256.25 in chips)
Seat 3: zzz (€200 in chips)
Seat 4: ddd (€200 in chips)
Seat 5: ccc (€223.40 in chips)
Seat 6: fff (€77.65 in chips)

file = open(r'example_hands.txt','r')
lines = [i.strip('\n') for i in file]
tab2 = {'Seat1': [],'S1_stack': [], 'Seat2': [], 'S2_stack':[]}
for i in range(len(lines)):
    tab2_2 = re.findall('Seat\s1\:\s(\w+)\s\(\$(\d+(\.\d+)?)\s',lines[i])
    for t2_2 in tab2_2:
        if tab2_2 is None:
            tab2['Seat1'].append(None)
            tab2['S1_stack'].append(None)
        else:
            tab2['Seat1'].append(t2_2[0])
            tab2['S1_stack'].append(t2_2[1])
    tab2_3 = re.findall('Seat\s2\:\s(\w+)\s\(\$(\d+(\.\d+)?)\s',linia[i])
    for t2_3 in tab2_3:
        tab2['Seat2'].append(t2_3[0])
        tab2['S2_stack'].append(t2_3[1])

目前我不能在列表中附加任何值。 有办法解决吗?有可能吗芬德尔先生? 我想要这样的输出:

enter image description here


Tags: noinforisstacklimitemhand
1条回答
网友
1楼 · 发布于 2024-06-26 10:56:53

您可以调整以下原型,它确实从您的文件中提取所有相关信息。你知道吗

作为作业,留给你的唯一一点就是以表格的方式显示它。你知道吗

import re

class PockerStarHand:
  def __init__(self, handId, date, seats):
    self.handId=handId
    self.date=date
    self.seats=seats

# does not scale, do this on small input files
with open('example_hands.txt','r') as content_file:
    content = content_file.read()
    chunck = re.findall(ur'^PokerStars\sHand\s#(\d+):.+?-(\s\d{4}\/\d{2}\/\d{2}\s\d{,2}:\d{2}:\d{2}).+\s+Table.+\s+^((?:Seat\s\d:\s\w+\s?\(.+\)\s*)+)',content, re.MULTILINE)
    hands = []
    for c in chunck:
      hands.append(PockerStarHand(c[0],c[1], re.findall(ur'^Seat\s*(\d)\s*\:\s*(\w+)\s*\(([\$\u20AC])(\d+(?:\.\d+)?)',c[2].decode('utf-8') ,re.MULTILINE | re.UNICODE)))

    for hand in hands:
      print ("ID: " + str(hand.handId))
      print ("date: " + str( hand.date))
      for s in hand.seats:
        print ("seat: " + str(s[0]))
        print ("seat text: " + str(s[1]))
        print ("seat curr: " + str(s[2].encode('utf-8')))
        print ("seat price: " + str(s[3]))

电流输出:

ID: 163417399919
date:  2016/12/23 23:48:52
seat: 1
seat text: xxx
seat curr: $
seat price: 200
seat: 2
seat text: yyy
seat curr: $
seat price: 364.58
seat: 3
seat text: zxc
seat curr: $
seat price: 200
seat: 4
seat text: zdf
seat curr: $
seat price: 235.43
seat: 5
seat text: zdasdasII
seat curr: $
seat price: 206.02
seat: 6
seat text: assfds
seat curr: $
seat price: 92.53
ID: 162960631727
date:  2016/12/15 2:10:16
seat: 2
seat text: xxx
seat curr: $
seat price: 137.08
seat: 3
seat text: yyy
seat curr: $
seat price: 200
seat: 5
seat text: xyz
seat curr: $
seat price: 201.20
ID: 163416930846
date:  2016/12/23 23:39:57
seat: 1
seat text: xxx
seat curr: €
seat price: 230.90
seat: 2
seat text: yyy
seat curr: €
seat price: 256.25
seat: 3
seat text: zzz
seat curr: €
seat price: 200
seat: 4
seat text: ddd
seat curr: €
seat price: 200
seat: 5
seat text: ccc
seat curr: €
seat price: 223.40
seat: 6
seat text: fff
seat curr: €
seat price: 77.65

正则表达式详细信息:

相关问题 更多 >