Python - get data from .txt file with regex

0

Hello, I'm trying to make a program but I'm not able to remove the data from the .txt file, it would look more or less bold.

PokerStars Hand #135235596385: Tournament #1228747530, $0.23+$0.02 USD Hold'em No Limit - Level I (10/20) - 2015/05/14 3:30:05 BRT [2015/05/14 2:30:05 ET]

Type .. I have to get the 'Hand' key with the data after '#' ending in ':', the same thing for 'Tournament' I get data after '#' and ending in ',' to save in a dictionary ...

d = {'Hand': 135235596385, 'Tournament': 1228747530}

It would be more or less that.

    
asked by anonymous 22.05.2015 / 20:02

2 answers

2

Given that the file can contain multiple lines like this, and the Hand value always comes before the Tournament value, you can do this:

>>> import re
>>> pokerstars = []
>>> with open('my_file.txt', 'r') as f:
...     for line in f:
...         data = (re.findall(r'#(\d+)[:,]', line))
...         pokerstars.append({'hand': data[0], 'tournament': data[1]})
... 
>>> pokerstars
[{'tournament': '1228747530', 'hand': '135235596385'}]

The most pythonica way to read a text file is through the context manager .

The findall method of the re package ( regex ) returns a list of hits that hit the specified regex.

    
25.05.2015 / 03:21
0

Just search for the digit sequence after the keywords. In your example:

>>> str = "PokerStars Hand #135235596385: Tournament #1228747530, $0.23+$0.02 USD Hold'em No Limit - Level I (10/20) - 2015/05/14 3:30:05 BRT [2015/05/14 2:30:05 ET]"

>>> d = {'Hand':       re.search(r'Hand #(\d+)', str).group(1),
         'Tournament': re.search(r'Tournament #(\d+)', str).group(1)}

>>> d
{'Tournament': '1228747530', 'Hand': '135235596385'}

The regex is fully explained in link

    
22.05.2015 / 21:13