Here is one program that might do what you want.
Note the use of parentheses ()
to isolate the data you are looking for. Note also the use of m.group(1)
, m.group(2)
to retrieve those saved items.
Note also the use of re.search()
instead of re.match()
. re.match()
must match the data from the very beginning of the string. re.search()
, on the other hand, will find the first match, regardless of its location in the string. (But also consider using re.findall()
, if a string might have multiple matches.).
Don't be confused by my use of .splitlines()
, it is just for the sake of the sample program. You could equally well do data = open('foo.txt')
/ for line in data:
.
import re
data = '''
(u'--UE_y6auTgq3FXlvUMkbw', 10),
(u'--XBxRlD92RaV6TyUnP8Ow', 1),
(u'--sSW-WY3vyASh_eVPGUAw', 2),
(u'-0GkcDiIgVm0XzDZC8RFOg', 9),
(u'-0OlcD1Ngv3yHXZE6KDlnw', 1),
(u'-0QBrNvhrPQCaeo7mTo0zQ', 1)
'''
data = data.splitlines()
for line in data:
m = re.search(r"'(.+)', (\d+)", line)
if m:
chars = m.group(1)
N = int(m.group(2))
print("I found a match!: {}, {}".format(chars, N))