gpt4 book ai didi

python - 当信息不存在时,是否可以使用 findall - 正则表达式附加 None 或任何值

转载 作者:行者123 更新时间:2023-12-01 08:19:34 24 4
gpt4 key购买 nike

我想解析文本并将其保存到数据帧。有时信息不存在,就像二手的那样,没有有关座位 1 的信息,而我想将 None 或任何值附加到字典中。

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

目前我无法将 None 或任何值附加到列表中。有什么想法如何解决这个问题吗? re.findall 可以吗?我想要这样的输出:

enter image description here

最佳答案

您可以调整以下原型(prototype),它确实从您的文件中提取所有相关信息。

作为作业留给你的唯一一点就是以表格的方式展示它。

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

正则表达式详细信息:

关于python - 当信息不存在时,是否可以使用 findall - 正则表达式附加 None 或任何值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54742936/

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