问题
I'm trying to find way to parse string that can contain variable, function, list, or dict written in python syntax separated with ",". Whitespace should be usable anywhere, so split with "," when its not inside (), [] or {}.
Example string: "variable, function1(1,3), function2([1,3],2), ['list_item_1','list_item_2'],{'dict_key_1': "dict_item_1"}"
Another example string: "variable,function1(1, 3) , function2( [1,3],2), ['list_item_1','list_item_2'],{'dict_key_1': "dict_item_1"}"
Example output ["variable", "function1(1,3)", "function2([1,3],2)", "['list_item_1','list_item_2']", "{'dict_key_1': "dict_item_1"}"]
edit:
Reason for the code is to parse string an then run it with exec("var = &s" % list[x])
. (yes i know this might not be recommended way to do stuff)
回答1:
Regular expressions aren't very good for parsing the complexity of arbitrary code. What exactly are you trying to accomplish? You can (unsafely) use eval
to just evaluate the string as code. Or if you're trying to understand it without eval
ing it, you can use the ast or dis modules for various forms of inspection.
回答2:
I guess the main problem here is that the arrays and dicts also have commas in them, so just using str.split(",")
wouldn't work. One way of doing it is to parse the string one character at a time, and keep track of whether all brackets are closed. If they are, we can append the current result to an array when we come across a comma. Here's my attempt:
s = "variable, function1(1,3),function2([1,3],2),['list_item_1','list_item_2'],{'dict_key_1': 'dict_item_1'}"
tokens = []
current = ""
open_brackets = 0
for char in s:
current += char
if char in "({[":
open_brackets += 1
elif char in ")}]":
open_brackets -= 1
elif (char == ",") and (open_brackets == 0):
tokens.append(current[:-1].strip())
current = ""
tokens.append(current)
for t in tokens:
print(t)
"""
variable
function1(1,3)
function2([1,3],2)
['list_item_1','list_item_2']
{'dict_key_1': 'dict_item_1'}
"""
回答3:
Have you tried using split?
>>> teststring = "variable, function1(1,3), function2([1,3],2), ['list_item_1','list_item_2'],{'dict_key_1': 'dict_item_1'}"
>>> teststring.split(", ")
['variable', 'function1(1,3)', 'function2([1,3],2)', "['list_item_1','list_item_2'],{'dict_key_1': 'dict_item_1'}"]
来源:https://stackoverflow.com/questions/39547056/python-parse-string-containing-functions-lists-and-dicts