【发布时间】:2011-08-06 20:59:04
【问题描述】:
//Last modified: Sat, Apr 16, 2011 09:55:04 AM
//Codeset: ISO-8859-1
fileInfo "version" "20x64";
createNode newnode -n "a_SET";
addAttr -ci true -k true -sn "connections" -ln "connections" -dt "string";
setAttr -l on -k off ".tx";
setAttr -l on -k off ".ty";
setAttr -l on -k off ".sz";
setAttr -l on -k on ".test1" -type "string" "blabla";
setAttr -l on -k on ".test2" -type "string" "blablabla";
createNode newnode -n "b_SET";
addAttr -ci true -k true -sn "connections" -ln "connections" -dt "string";
setAttr -l on -k off ".tx";
setAttr -l on -k off ".ty";
setAttr -l on -k off ".sz";
setAttr -l on -k on ".test1" -type "string" "hmm";
setAttr -l on -k on ".test2" -type "string" "ehmehm";
在 Python 中:
我需要读取新节点名称,例如“a_SET”和“b_SET”及其对应的属性值,所以 {"a_SET": {"test1":"blabla", "test2":"blablabla"} 和相同对于 b_SET - 可能存在未知数量的集合 - 例如 c_SET d_SET 等。
我试过循环遍历行并在那里匹配:
for line in fileopened:
setmatch = re.match( r'^(createNode set -n ")(.*)(_SET)(.*)' , line)
if setmatch:
sets.append(setmatch.group(2))
一旦我在这里找到匹配项,我将遍历下一行以获取该集合的属性(test1、test2),直到找到一个新集合 - 例如 c_SET 或 EOF。
使用 re.MULTILINE 一次性获取所有信息的最佳方式是什么?
【问题讨论】:
标签: python regex file match multiline