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
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
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'}
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]+)\]