看起来像有效的集合表示法,所以你可以使用 ast 模块来解析它:
import ast
mystr = 'myString={"name", "age", "address", "contacts", "Email"}'
tree = ast.parse(mystr)
name = tree.body[0].targets[0].id
values = [val.s for val in tree.body[0].value.elts]
print name, values
# prints: myString ['name', 'age', 'address', 'contacts', 'Email']
编辑:根据输入文件的实际格式,我会使用正则表达式来解析块,然后像上面那样解析块,或者像下面这样去掉引号:
import re
block_re = re.compile(r'v_dims=\{(.*?)\}', re.S)
with open("C:\XXXX\nemo\Test.mrk") as f:
doc = f.read()
block = block_re.search(doc)
[s.strip().strip('"') for s in block.group(1).split(',')]
但可能最好的方法是将两者结合起来:
import ast
import re
with open("C:\XXXX\nemo\Test.mrk") as f:
doc = f.read()
block_re = re.compile(r'v_dims=\{.*?\}', re.S)
tree = ast.parse(block_re.search(doc).group(0))
print [val.s for val in tree.body[0].value.elts]
# ['name', 'age', 'address', 'contacts', 'Email']