Python:解析文本文件

时间:2011-09-18 03:11:09

标签: python parsing

我的文件如下:

2乘100的矩阵,

我想为每列创建一个列表,列表对应于每行映射到当前的第一个元素,第二个元素映射到温度。

如下图所示。有什么更好的方法让代码看起来更漂亮?

-12,30
-34,50
-33,89
-900,9
-2,37
-7,17
-8,28
-12,30
-34,50
-33,89

def parse_log(fname):
    f = open(fname, "r")
    samples = f.readlines()
    samples = filter(lambda x: not x.startswith('*'), samples)
    print(samples)
    current = map(lambda x: -1 * int(x.split(',')[0]), samples)
    print(current)
    temperature = map(lambda x: int(x.split(',')[1]), samples)
    print(temperature)
    return (current, temperature)

3 个答案:

答案 0 :(得分:1)

为避免每行都进行两次split调用,我建议采用以下解决方案

def parse_log(fname):
    with open(fname) as f:
        samples = [line.strip() for line in f.readlines()
                   if not line.startswith('*')]
        ints = [map(int, line.split(",")) for line in samples]
        currents = [-x[0] for x in ints]
        temperatures = [x[1] for x in ints]
        return currents, temperatures

答案 1 :(得分:1)

这是一个简单的版本,IMO可以合理地达到几兆字节的日志文件(它不会尝试在解析期间最小化内存使用或计算时间):

 def parse_log(fname):
     data = [map(int, x.split(",")) for x in open(fname) if x[:1] != "*"]
     return ([-current for current, temp in data],
             [temp for current, temp in data])

答案 2 :(得分:0)

使用生成器表达式:

def parse_log(fname):
    with open(fname, "r") as file:
        return zip(*(
            (int(current) * -1, int(temp)) for current, temp in
                (line.strip().split(',')
                    for line in file if not line.startswith('*'))
        ))

print parse_log(filename)

[(-12, -34, -33, -900, -2, -7, -8, -12, -34, -33), (30, 50, 89, 9, 37, 17, 28, 30, 50, 89)]

警告,这不一定更好,因为它可能更难以阅读和理解正在发生的事情。请务必使用docstring正确记录。