Вы можете написать себе синтаксический анализатор (хотя, возможно, немного перегружен):
from parsimonious.grammar import Grammar
from parsimonious.nodes import NodeVisitor
text = """
"userType": "NORMAL", "accountID": "J123456789"
"userType": "NORMAL", "accountID": "J987654321"
"userType": "NORMAL", "accountID": "C123456789"
"userType": "NORMAL", "accountID": "R987654321"
"""
grammar = Grammar(
r"""
file = entry+
entry = garbage? (pair)+ newline
pair = ws? key equal value comma?
key = quotes word quotes
value = quotes word quotes
quotes = '"'
word = ~"\w+"
equal = ws? ":" ws?
comma = ws? "," ws?
ws = ~"[\t ]+"
newline = ~"[\r\n]"
garbage = (ws / newline)+
"""
)
tree = grammar.parse(text)
class Vistor(NodeVisitor):
def __init__(self, needle):
self.needle = needle
def generic_visit(self, node, visited_children):
return visited_children or node
def visit_key(self, node, children):
_, key, _ = children
return key
def visit_value(self, node, children):
_, value, _ = children
return value
def visit_pair(self, node, children):
_, key, _, value, _ = children
return (key, value)
def visit_entry(self, node, children):
_, entry, _ = children
return entry
def visit_file(self, node, children):
out = [value.text
for child in children if isinstance(child, list)
for key, value in child
if key.text == self.needle]
return out
v = Vistor("accountID")
out = v.visit(tree)
print(out)
Что дает
['J123456789', 'J987654321', 'C123456789', 'R987654321']