这是一个创建于 4288 天前的主题,其中的信息可能已经有所发展或是发生改变。
我有一串长长的:
for line in open('.txt'):
m = re.match(exp1, line, re.IGNORECASE)
if m:
func1(m.group())
continue
m = re.match(exp2, line, re.IGNORECASE)
if m:
func2(m.group())
continue
m = re.match(exp3, line, re.IGNORECASE)
if m:
func3(m.group())
continue
......
一来有一堆的正则解析和处理,二来别的别的地方还会调用其中几条,因而求教该怎么化简?
最初的想法是弄一个字典包含像exp1:func1, exp2:func2,然后一个for逐条解析,但是字典是无序的,因而不能按照上面的顺序一条一条跑。
然后觉得可以新建一个类,每个解析方法各弄一条子类,但是好像要写很多的样子……
总之想看看大神们怎么写的……
4 条回复 • 1970-01-01 08:00:00 +08:00
|
|
1
reusFork 2012-12-24 00:11:36 +08:00 1
rules = [(exp1, func1), (exp2, func2), (exp3, func3)] for l in lines: for exp, func in rules: if …………
|
|
|
2
wwwjfy 2012-12-24 00:21:12 +08:00
python有个东西叫OrderredDict
|
|
|
3
wwwjfy 2012-12-24 00:21:43 +08:00
写错了,OrderedDict
|
|
|
4
ThunderEX 2012-12-24 00:32:22 +08:00 via Android
@ reusFork 唔…是我把问题想复杂了…好像新手都会把问题想复杂?为什么…
|