1

Is there a quick way to extract the argument-value pairs from below string in Python?

s = '[ARG0: elephants] were [ARGM-TMP: first] [V: walk] [ARGM-DIR: up]'

Output is in the form below:

ARG0 = elephants
ARGM-TMP = first
ARGM-DIR = up
tstseby
  • 837
  • 3
  • 6
  • 17

2 Answers2

3

You could find all arg/value pairs using re.findall and build a dictionary by splitting the strings:

import re
s = '[ARG0: elephants] were [ARGM-TMP: first] [V: walk] [ARGM-DIR: up]'

d = re.findall(r'\[(ARG.*?\:*?)]', s)

dict([i.split(': ') for i in d])
# {'ARG0': 'elephants', 'ARGM-TMP': 'first', 'ARGM-DIR': 'up'}
yatu
  • 75,195
  • 11
  • 47
  • 89
1

Read up on regex in python. And you can use a regex string like the following.

\[(?P<key>[a-zA-Z0-9-_]+): (?P<value>[a-z]+)\]
Bloodmallet
  • 364
  • 1
  • 9