匿名用户
1级
2013-05-06 回答
#!/usr/bin/env python
# coding: utf-8
import os
import re
def parserln(ln, patt):
"""用给定的正则表达式解析行"""
matched = patt.match(ln)
if matched:
return matched.groupdict()
def getdata(filename, parser, callback=None):
"""用指定的解析方法parser解析指定文件,
用callback进行数据加工过的数据列表
"""
with open(filename, 'rt') as handle:
return map(
callback,
filter(None, map(parser, handle))
)
def storage(filename, dataserial, spliter=','):
"""将数据序列按行存储到指定文件,
每一序列元素间用指定的字符分割"""
with open(filename, 'wt') as handle:
handle.writelines([
"%s\n" % (spliter.join(map(str, item)))
for item in dataserial
])