I am at the very beginning with python and parsing text files. So, I am not sure yet what the problem is and what to change in my code.
Here's my code:
import re
filepath = 'data.txt'
rx_data = {
'teams': re.compile(r'\bTeamVitality\s[6 - 16]\s\bNAVI\sGGBET'),
'rounds_played': re.compile(r'\bRoundsPlayed:\s22'),
'all_time': re.compile(r'\b50\smin'),
'map': re.compile(r'\bde_nuke'),
'won': re.compile(r'\bTeam\sTeamVitality\swon')
}
def _parse_line(line):
for key, rx in rx_data.items():
print(rx)
match = rx.search(line)
if match:
return key, match
return None, None
def parse_file(filepath):
data = {}
with open(filepath, 'r') as text_file:
line = text_file.readline()
while line:
key, match = _parse_line(line)
if key == 'teams':
setattr(data, 'teams', match.group())
if key == 'rounds_played':
setattr(data, 'rounds_played', match.group())
if key == 'all_time':
setattr(data, 'all_time', match.group())
if key == 'map':
setattr(data, 'map', match.group())
return data
if __name__ == '__main__':
data = parse_file(filepath)
print(data)
Here is the example with part of the data.txt:
11/28/2021 - 21:30:01: "b1t<35><STEAM_1:0:143170874><CT>" [943 -1206 -640] attacked "shox <33><STEAM_1:1:23327283><TERRORIST>" [296 -1310 -768] with "famas" (damage "24") (damage_armor "5") (health "57") (armor "89") (hitgroup "stomach")
11/28/2021 - 21:30:02: "b1t<35><STEAM_1:0:143170874><CT>" [942 -1205 -640] attacked "shox <33><STEAM_1:1:23327283><TERRORIST>" [298 -1313 -768] with "famas" (damage "19") (damage_armor "4") (health "38") (armor "84") (hitgroup "chest")
11/28/2021 - 21:30:02: "b1t<35><STEAM_1:0:143170874><CT>" [942 -1204 -640] attacked "shox <33><STEAM_1:1:23327283><TERRORIST>" [298 -1312 -768] with "famas" (damage "79") (damage_armor "17") (health "0") (armor "66") (hitgroup "head")
11/28/2021 - 21:30:02: "shox <33><STEAM_1:1:23327283><TERRORIST>" dropped "ak47"
11/28/2021 - 21:30:02: "shox <33><STEAM_1:1:23327283><TERRORIST>" dropped "vesthelm"
11/28/2021 - 21:30:02: "shox <33><STEAM_1:1:23327283><TERRORIST>" dropped "knife"
11/28/2021 - 21:30:02: "shox <33><STEAM_1:1:23327283><TERRORIST>" dropped "glock"
11/28/2021 - 21:30:02: "shox <33><STEAM_1:1:23327283><TERRORIST>" dropped "flashbang"
11/28/2021 - 21:30:02: "b1t<35><STEAM_1:0:143170874><CT>" [942 -1204 -640] killed "shox <33><STEAM_1:1:23327283><TERRORIST>" [298 -1312 -718] with "famas" (headshot)
11/28/2021 - 21:30:02: "b1t<13><STEAM_1:0:143170874><CT>" money change 500 300 = $800 (tracked)
11/28/2021 - 21:30:02: "s1mple<30><STEAM_1:1:36968273><CT>" [937 -265 -640] attacked "b1t<35><STEAM_1:0:143170874><CT>" [943 -1204 -640] with "m4a1" (damage "10") (damage_armor "2") (health "90") (armor "88") (hitgroup "stomach")
11/28/2021 - 21:30:02: "misutaaa<24><STEAM_1:1:60631591><TERRORIST>" [947 -445 -640] attacked "s1mple<30><STEAM_1:1:36968273><CT>" [937 -265 -640] with "ak47" (damage "27") (damage_armor "4") (health "73") (armor "95") (hitgroup "chest")
11/28/2021 - 21:30:02: "s1mple<30><STEAM_1:1:36968273><CT>" [933 -265 -640] attacked "b1t<35><STEAM_1:0:143170874><CT>" [939 -1212 -640] with "m4a1" (damage "9") (damage_armor "0") (health "81") (armor "88") (hitgroup "left leg")
11/28/2021 - 21:30:02: "misutaaa<24><STEAM_1:1:60631591><TERRORIST>" [949 -443 -640] attacked "s1mple<30><STEAM_1:1:36968273><CT>" [933 -265 -640] with "ak47" (damage "27") (damage_armor "4") (health "46") (armor "90") (hitgroup "chest")
11/28/2021 - 21:30:02: "misutaaa<24><STEAM_1:1:60631591><TERRORIST>" [951 -441 -640] attacked "s1mple<30><STEAM_1:1:36968273><CT>" [933 -265 -640] with "ak47" (damage "27") (damage_armor "4") (health "19") (armor "85") (hitgroup "chest")
11/28/2021 - 21:30:02: "s1mple<30><STEAM_1:1:36968273><CT>" [934 -265 -640] attacked "misutaaa<24><STEAM_1:1:60631591><TERRORIST>" [952 -440 -640] with "m4a1" (damage "92") (damage_armor "19") (health "8") (armor "70") (hitgroup "head")
11/28/2021 - 21:30:03: "misutaaa<24><STEAM_1:1:60631591><TERRORIST>" [953 -439 -640] attacked "s1mple<30><STEAM_1:1:36968273><CT>" [934 -265 -640] with "ak47" (damage "110") (damage_armor "16") (health "0") (armor "68") (hitgroup "head")
11/28/2021 - 21:30:03: "s1mple<30><STEAM_1:1:36968273><CT>" dropped "defuser"
11/28/2021 - 21:30:03: "s1mple<30><STEAM_1:1:36968273><CT>" dropped "m4a1"
11/28/2021 - 21:30:03: "s1mple<30><STEAM_1:1:36968273><CT>" dropped "vesthelm"
11/28/2021 - 21:30:03: "s1mple<30><STEAM_1:1:36968273><CT>" dropped "knife"
11/28/2021 - 21:30:03: "s1mple<30><STEAM_1:1:36968273><CT>" dropped "hkp2000"
11/28/2021 - 21:30:03: "misutaaa<24><STEAM_1:1:60631591><TERRORIST>" [953 -439 -640] killed "s1mple<30><STEAM_1:1:36968273><CT>" [934 -265 -593] with "ak47" (headshot)
11/28/2021 - 21:30:03: "misutaaa<5><STEAM_1:1:60631591><TERRORIST>" money change 6850 300 = $7150 (tracked)
11/28/2021 - 21:30:03: "b1t<35><STEAM_1:0:143170874><CT>" [933 -1288 -640] attacked "misutaaa<24><STEAM_1:1:60631591><TERRORIST>" [957 -432 -640] with "famas" (damage "78") (damage_armor "16") (health "0") (armor "53") (hitgroup "head")
11/28/2021 - 21:30:03: "misutaaa<24><STEAM_1:1:60631591><TERRORIST>" dropped "ak47"
11/28/2021 - 21:30:03: "misutaaa<24><STEAM_1:1:60631591><TERRORIST>" dropped "vesthelm"
11/28/2021 - 21:30:03: "misutaaa<24><STEAM_1:1:60631591><TERRORIST>" dropped "knife"
11/28/2021 - 21:30:03: "misutaaa<24><STEAM_1:1:60631591><TERRORIST>" dropped "glock"
11/28/2021 - 21:30:03: "misutaaa<24><STEAM_1:1:60631591><TERRORIST>" dropped "flashbang"
11/28/2021 - 21:30:03: "b1t<35><STEAM_1:0:143170874><CT>" [933 -1288 -640] killed "misutaaa<24><STEAM_1:1:60631591><TERRORIST>" [957 -432 -576] with "famas" (headshot)
11/28/2021 - 21:30:03: "s1mple<30><STEAM_1:1:36968273><CT>" assisted killing "misutaaa<24><STEAM_1:1:60631591><TERRORIST>"
11/28/2021 - 21:30:03: "b1t<13><STEAM_1:0:143170874><CT>" money change 800 300 = $1100 (tracked)
11/28/2021 - 21:30:04: "ZywOo<26><STEAM_1:1:76700232><TERRORIST>" [962 -584 -640] attacked "b1t<35><STEAM_1:0:143170874><CT>" [815 -1315 -640] with "ak47" (damage "27") (damage_armor "3") (health "54") (armor "84") (hitgroup "chest")
11/28/2021 - 21:30:04: "ZywOo<26><STEAM_1:1:76700232><TERRORIST>" [966 -585 -640] attacked "b1t<35><STEAM_1:0:143170874><CT>" [803 -1312 -640] with "ak47" (damage "27") (damage_armor "3") (health "27") (armor "80") (hitgroup "chest")
11/28/2021 - 21:30:04: "ZywOo<26><STEAM_1:1:76700232><TERRORIST>" [966 -585 -640] attacked "b1t<35><STEAM_1:0:143170874><CT>" [798 -1313 -640] with "ak47" (damage "27") (damage_armor "3") (health "0") (armor "76") (hitgroup "chest")
11/28/2021 - 21:30:04: "b1t<35><STEAM_1:0:143170874><CT>" dropped "famas"
11/28/2021 - 21:30:04: "b1t<35><STEAM_1:0:143170874><CT>" dropped "vesthelm"
11/28/2021 - 21:30:04: "b1t<35><STEAM_1:0:143170874><CT>" dropped "knife"
11/28/2021 - 21:30:04: "b1t<35><STEAM_1:0:143170874><CT>" dropped "hkp2000"
11/28/2021 - 21:30:04: "b1t<35><STEAM_1:0:143170874><CT>" dropped "flashbang"
11/28/2021 - 21:30:04: "ZywOo<26><STEAM_1:1:76700232><TERRORIST>" [966 -585 -640] killed "b1t<35><STEAM_1:0:143170874><CT>" [798 -1313 -576] with "ak47"
11/28/2021 - 21:30:04: "ZywOo<7><STEAM_1:1:76700232><TERRORIST>" money change 10100 300 = $10400 (tracked)
11/28/2021 - 21:30:08: "ZywOo<26><STEAM_1:1:76700232><TERRORIST>" picked up "c4"
11/28/2021 - 21:30:08: "ZywOo<26><STEAM_1:1:76700232><TERRORIST>" triggered "Got_The_Bomb"
11/28/2021 - 21:30:08: "ZywOo<26><STEAM_1:1:76700232><TERRORIST>" picked up "molotov"
11/28/2021 - 21:30:09: "ZywOo<26><STEAM_1:1:76700232><TERRORIST>" triggered "Bomb_Begin_Plant" at bombsite B
11/28/2021 - 21:30:12: "electronic<31><STEAM_1:1:41889689><CT>" picked up "flashbang"
11/28/2021 - 21:30:12: "ZywOo<7><STEAM_1:1:76700232><TERRORIST>" money change 10400 300 = $10700 (tracked)
11/28/2021 - 21:30:12: "ZywOo<26><STEAM_1:1:76700232><TERRORIST>" triggered "Planted_The_Bomb" at bombsite B
11/28/2021 - 21:30:12: "ZywOo<26><STEAM_1:1:76700232><TERRORIST>" dropped "c4"
11/28/2021 - 21:30:14: "ZywOo<26><STEAM_1:1:76700232><TERRORIST>" [740 -1147 -769] attacked "Boombl4<29><STEAM_1:0:92970669><CT>" [1136 -908 -717] with "ak47" (damage "26") (damage_armor "0") (health "54") (armor "96") (hitgroup "left leg")
11/28/2021 - 21:30:14: "Boombl4<29><STEAM_1:0:92970669><CT>" [1132 -884 -730] attacked "ZywOo<26><STEAM_1:1:76700232><TERRORIST>" [739 -1146 -769] with "mp9" (damage "13") (damage_armor "4") (health "87") (armor "76") (hitgroup "chest")
11/28/2021 - 21:30:14: "ZywOo<26><STEAM_1:1:76700232><TERRORIST>" [739 -1146 -769] attacked "Boombl4<29><STEAM_1:0:92970669><CT>" [1132 -883 -730] with "ak47" (damage "34") (damage_armor "4") (health "20") (armor "91") (hitgroup "stomach")
11/28/2021 - 21:30:14: "Boombl4<29><STEAM_1:0:92970669><CT>" [1129 -880 -744] attacked "ZywOo<26><STEAM_1:1:76700232><TERRORIST>" [739 -1145 -769] with "mp9" (damage "17") (damage_armor "5") (health "70") (armor "70") (hitgroup "stomach")
11/28/2021 - 21:30:15: "ZywOo<26><STEAM_1:1:76700232><TERRORIST>" [740 -1146 -769] attacked "Boombl4<29><STEAM_1:0:92970669><CT>" [1127 -880 -752] with "ak47" (damage "27") (damage_armor "3") (health "0") (armor "87") (hitgroup "left arm")
11/28/2021 - 21:30:15: "Boombl4<29><STEAM_1:0:92970669><CT>" dropped "defuser"
11/28/2021 - 21:30:15: "Boombl4<29><STEAM_1:0:92970669><CT>" dropped "mp9"
11/28/2021 - 21:30:15: "Boombl4<29><STEAM_1:0:92970669><CT>" dropped "vesthelm"
11/28/2021 - 21:30:15: "Boombl4<29><STEAM_1:0:92970669><CT>" dropped "knife"
11/28/2021 - 21:30:15: "Boombl4<29><STEAM_1:0:92970669><CT>" dropped "hkp2000"
11/28/2021 - 21:30:15: "ZywOo<26><STEAM_1:1:76700232><TERRORIST>" [740 -1146 -769] killed "Boombl4<29><STEAM_1:0:92970669><CT>" [1127 -880 -688] with "ak47"
11/28/2021 - 21:30:15: "ZywOo<7><STEAM_1:1:76700232><TERRORIST>" money change 10700 300 = $11000 (tracked)
11/28/2021 - 21:30:15: "ZywOo<26><STEAM_1:1:76700232><TERRORIST>" [739 -1146 -769] attacked "Perfecto<28><STEAM_1:0:80477379><CT>" [1050 -965 -768] with "ak47" (damage "27") (damage_armor "3") (health "73") (armor "96") (hitgroup "right arm")
11/28/2021 - 21:30:15: "Perfecto<28><STEAM_1:0:80477379><CT>" [1042 -955 -768] attacked "ZywOo<26><STEAM_1:1:76700232><TERRORIST>" [739 -1145 -769] with "famas" (damage "20") (damage_armor "4") (health "50") (armor "65") (hitgroup "chest")
11/28/2021 - 21:30:15: "ZywOo<26><STEAM_1:1:76700232><TERRORIST>" [735 -1138 -769] attacked "Perfecto<28><STEAM_1:0:80477379><CT>" [1042 -955 -768] with "ak47" (damage "27") (damage_armor "3") (health "46") (armor "92") (hitgroup "chest")
11/28/2021 - 21:30:15: "ZywOo<26><STEAM_1:1:76700232><TERRORIST>" [734 -1137 -769] attacked "Perfecto<28><STEAM_1:0:80477379><CT>" [1043 -956 -768] with "ak47" (damage "142") (damage_armor "0") (health "0") (armor "92") (hitgroup "head")
11/28/2021 - 21:30:15: "Perfecto<28><STEAM_1:0:80477379><CT>" dropped "famas"
11/28/2021 - 21:30:15: "Perfecto<28><STEAM_1:0:80477379><CT>" dropped "knife"
11/28/2021 - 21:30:15: "Perfecto<28><STEAM_1:0:80477379><CT>" dropped "hkp2000"
11/28/2021 - 21:30:15: "ZywOo<26><STEAM_1:1:76700232><TERRORIST>" [734 -1137 -769] killed "Perfecto<28><STEAM_1:0:80477379><CT>" [1043 -956 -722] with "ak47" (headshot)
11/28/2021 - 21:30:15: "ZywOo<7><STEAM_1:1:76700232><TERRORIST>" money change 11000 300 = $11300 (tracked)
11/28/2021 - 21:30:17: "ZywOo<26><STEAM_1:1:76700232><TERRORIST>" [715 -1029 -772] attacked "electronic<31><STEAM_1:1:41889689><CT>" [341 -1244 -768] with "ak47" (damage "141") (damage_armor "0") (health "0") (armor "87") (hitgroup "head")
11/28/2021 - 21:30:17: "electronic<31><STEAM_1:1:41889689><CT>" dropped "m4a1"
11/28/2021 - 21:30:17: "electronic<31><STEAM_1:1:41889689><CT>" dropped "flashbang"
11/28/2021 - 21:30:17: "electronic<31><STEAM_1:1:41889689><CT>" dropped "knife"
11/28/2021 - 21:30:17: "electronic<31><STEAM_1:1:41889689><CT>" dropped "hkp2000"
11/28/2021 - 21:30:17: "ZywOo<26><STEAM_1:1:76700232><TERRORIST>" [715 -1029 -772] killed "electronic<31><STEAM_1:1:41889689><CT>" [341 -1244 -706] with "ak47" (headshot)
11/28/2021 - 21:30:17: "ZywOo<7><STEAM_1:1:76700232><TERRORIST>" money change 11300 300 = $11600 (tracked)
11/28/2021 - 21:30:17: Team "TERRORIST" triggered "SFUI_Notice_Terrorists_Win" (CT "6") (T "16")
11/28/2021 - 21:30:17: Team "CT" scored "6" with "5" players
11/28/2021 - 21:30:17: Team "TERRORIST" scored "16" with "5" players
11/28/2021 - 21:30:17: MatchStatus: Team playing "CT": NAVI GGBET
11/28/2021 - 21:30:17: MatchStatus: Team playing "TERRORIST": TeamVitality
11/28/2021 - 21:30:17: MatchStatus: Score: 6:16 on map "de_nuke" RoundsPlayed: 22
11/28/2021 - 21:30:17: World triggered "Round_End"
11/28/2021 - 21:30:17: [FACEIT^] NAVI GGBET [6 - 16] TeamVitality
11/28/2021 - 21:30:17: "misutaaa<5><STEAM_1:1:60631591><TERRORIST>" money change 7150 3250 = $10400 (tracked)
11/28/2021 - 21:30:17: "apEX<6><STEAM_1:1:14739219><TERRORIST>" money change 8900 3250 = $12150 (tracked)
11/28/2021 - 21:30:17: "ZywOo<7><STEAM_1:1:76700232><TERRORIST>" money change 11600 3250 = $14850 (tracked)
11/28/2021 - 21:30:17: "Kyojin<8><STEAM_1:1:22851120><TERRORIST>" money change 7050 3250 = $10300 (tracked)
11/28/2021 - 21:30:17: "shox <14><STEAM_1:1:23327283><TERRORIST>" money change 2900 3250 = $6150 (tracked)
11/28/2021 - 21:30:17: "Perfecto<9><STEAM_1:0:80477379><CT>" money change 0 3400 = $3400 (tracked)
11/28/2021 - 21:30:17: "Boombl4<10><STEAM_1:0:92970669><CT>" money change 150 3400 = $3550 (tracked)
11/28/2021 - 21:30:17: "s1mple<11><STEAM_1:1:36968273><CT>" money change 500 3400 = $3900 (tracked)
11/28/2021 - 21:30:17: "electronic<12><STEAM_1:1:41889689><CT>" money change 450 3400 = $3850 (tracked)
11/28/2021 - 21:30:17: "b1t<13><STEAM_1:0:143170874><CT>" money change 1100 3400 = $4500 (tracked)
11/28/2021 - 21:30:17: ACCOLADE, FINAL: {pistolkills}, Boombl4<10>, VALUE: 5.000000, POS: 1, SCORE: 2.629870
11/28/2021 - 21:30:17: ACCOLADE, FINAL: {burndamage}, s1mple<11>, VALUE: 97.000000, POS: 1, SCORE: 25.196194
11/28/2021 - 21:30:17: ACCOLADE, FINAL: {firstkills}, electronic<12>, VALUE: 4.000000, POS: 1, SCORE: 6.363637
11/28/2021 - 21:30:17: ACCOLADE, FINAL: {hsp}, b1t<13>, VALUE: 82.352943, POS: 1, SCORE: 56.274513
11/28/2021 - 21:30:17: ACCOLADE, FINAL: {kills}, Perfecto<9>, VALUE: 10.000000, POS: 3, SCORE: 0.751392
11/28/2021 - 21:30:17: ACCOLADE, FINAL: {3k}, apEX<6>, VALUE: 3.000000, POS: 1, SCORE: 80.000000
11/28/2021 - 21:30:17: ACCOLADE, FINAL: {4k}, ZywOo<7>, VALUE: 1.000000, POS: 1, SCORE: 40.000000
11/28/2021 - 21:30:17: ACCOLADE, FINAL: {hsp}, Kyojin<8>, VALUE: 60.000000, POS: 1, SCORE: 38.888893
11/28/2021 - 21:30:17: ACCOLADE, FINAL: {cashspent}, shox <14>, VALUE: 62300.000000, POS: 1, SCORE: 10.467534
11/28/2021 - 21:30:17: ACCOLADE, FINAL: {burndamage}, misutaaa<5>, VALUE: 52.000000, POS: 2, SCORE: 7.070709
11/28/2021 - 21:30:17: MatchStatus: Team playing "CT": NAVI GGBET
11/28/2021 - 21:30:17: MatchStatus: Team playing "TERRORIST": TeamVitality
11/28/2021 - 21:30:17: MatchStatus: Score: 6:16 on map "de_nuke" RoundsPlayed: 22
11/28/2021 - 21:30:17: Game Over: competitive 1092904694 de_nuke score 6:16 after 50 min
11/28/2021 - 21:30:17: "misutaaa<24><STEAM_1:1:60631591><TERRORIST>" has 10400$
11/28/2021 - 21:30:17: "apEX<25><STEAM_1:1:14739219><TERRORIST>" has 12150$
11/28/2021 - 21:30:17: "ZywOo<26><STEAM_1:1:76700232><TERRORIST>" has 14850$
11/28/2021 - 21:30:17: "Kyojin<34><STEAM_1:1:22851120><TERRORIST>" has 10300$
11/28/2021 - 21:30:17: "Perfecto<28><STEAM_1:0:80477379><CT>" has 3400$
11/28/2021 - 21:30:17: "Boombl4<29><STEAM_1:0:92970669><CT>" has 3550$
11/28/2021 - 21:30:17: "s1mple<30><STEAM_1:1:36968273><CT>" has 3900$
11/28/2021 - 21:30:17: "electronic<31><STEAM_1:1:41889689><CT>" has 3850$
11/28/2021 - 21:30:17: "b1t<35><STEAM_1:0:143170874><CT>" has 4500$
11/28/2021 - 21:30:17: "shox <33><STEAM_1:1:23327283><TERRORIST>" has 6150$
11/28/2021 - 21:30:17: [FACEIT^] Team TeamVitality won.
11/28/2021 - 21:30:17: World triggered "Round_Start"
11/28/2021 - 21:30:34: [FACEIT^] Please wait at least 30 sec before the next map.
11/28/2021 - 21:30:36: [FACEIT] Admin service cancelled the match with reason 1
11/28/2021 - 21:30:36: [FACEIT^] An admin has cancelled the match.
11/28/2021 - 21:30:51: "prius<22><STEAM_1:1:17960540><Spectator>" disconnected (reason "The match has been cancelled")
11/28/2021 - 21:30:51: "Linaaa<23><STEAM_1:1:37563905><Spectator>" disconnected (reason "The match has been cancelled")
11/28/2021 - 21:30:51: "misutaaa<24><STEAM_1:1:60631591><TERRORIST>" disconnected (reason "The match has been cancelled")
11/28/2021 - 21:30:51: "apEX<25><STEAM_1:1:14739219><TERRORIST>" disconnected (reason "The match has been cancelled")
11/28/2021 - 21:30:51: "ZywOo<26><STEAM_1:1:76700232><TERRORIST>" disconnected (reason "The match has been cancelled")
11/28/2021 - 21:30:51: "Kyojin<34><STEAM_1:1:22851120><TERRORIST>" disconnected (reason "The match has been cancelled")
11/28/2021 - 21:30:51: "Perfecto<28><STEAM_1:0:80477379><CT>" disconnected (reason "The match has been cancelled")
11/28/2021 - 21:30:51: "Boombl4<29><STEAM_1:0:92970669><CT>" disconnected (reason "The match has been cancelled")
11/28/2021 - 21:30:51: "s1mple<30><STEAM_1:1:36968273><CT>" disconnected (reason "The match has been cancelled")
11/28/2021 - 21:30:51: "electronic<31><STEAM_1:1:41889689><CT>" disconnected (reason "The match has been cancelled")
11/28/2021 - 21:30:51: "b1t<35><STEAM_1:0:143170874><CT>" disconnected (reason "The match has been cancelled")
11/28/2021 - 21:30:51: "shox <33><STEAM_1:1:23327283><TERRORIST>" disconnected (reason "The match has been cancelled")
11/28/2021 - 21:30:53: Your server needs to be restarted in order to receive the latest update.
11/28/2021 - 21:31:03: Your server needs to be restarted in order to receive the latest update.
11/28/2021 - 21:31:13: "GOTV<20><BOT><Unassigned>" disconnected (reason "GOTV stop.")
11/28/2021 - 21:31:13: [FACEIT] Uploading GOTV demo
11/28/2021 - 21:31:23: Your server needs to be restarted in order to receive the latest update.
11/28/2021 - 21:31:37: Your server needs to be restarted in order to receive the latest update.
11/28/2021 - 21:31:38: [FACEIT] Blocked map de_nuke reserved command
11/28/2021 - 21:31:38: "misutaaa<36><STEAM_1:1:60631591><>" connected, address ""
11/28/2021 - 21:31:38: "misutaaa<36><STEAM_1:1:60631591><>" disconnected (reason "Your SteamID is not allowed")
11/28/2021 - 21:31:49: Your server needs to be restarted in order to receive the latest update.
print(data) gives me exactly the same re.compile(regex) from rx_data and it's not stopping till I will exit.
Expected output:
data = {
teams: [TeamVitality, NAVI GGBET],
map: de_nuke,
score: 6 - 16,
all_time: 50,
rounds_played: 22,
average_round_length: x = (all_time rounds_played)/2
etc...
}
I would be very grateful for some help, direction, and what to focus on...
CodePudding user response:
Something like this? Consider that YOU have to decide how to structure the "object" you want as a result. Here I just made a dictionary having the same keys as rx_data
a list of tuple as a value. Each tuple contains the line_number
and its match.
import re
from collections import defaultdict
rx_data = {
'teams': re.compile(r'\bTeamVitality\s[6 - 16]\s\bNAVI\sGGBET'),
'rounds_played': re.compile(r'\bRoundsPlayed:\s22'),
'all_time': re.compile(r'\b50\smin'),
'map': re.compile(r'\bde_nuke'),
'won': re.compile(r'\bTeam\sTeamVitality\swon')
}
def parse_line(line, rx_dict):
for key, rx in rx_dict.items():
match = rx.search(line)
if match:
return key, match
return None
obj = defaultdict(list)
with open('untitled.txt', 'r') as f:
for line_number, line in enumerate(f):
match = parse_line(line, rx_data)
if match:
print(f'found match at line {line_number}: {match[0]} >> {match[1].group()}')
obj[match[0]].append((line_number, match[1].group())) #store line_number and match
Output:
found match at line 84: rounds_played >> RoundsPlayed: 22
found match at line 109: rounds_played >> RoundsPlayed: 22
found match at line 110: all_time >> 50 min
found match at line 121: won >> Team TeamVitality won
found match at line 144: map >> de_nuke
And the object:
print(obj)
defaultdict(<class 'list'>, {'rounds_played': [(84, 'RoundsPlayed: 22'), (109, 'RoundsPlayed: 22')], 'all_time': [(110, '50 min')], 'won': [(121, 'Team TeamVitality won')], 'map': [(144, 'de_nuke')]})