简体   繁体   中英

Python: Parse Text File

My file looks like this:

A matrix of 2 by 100 ,

I would like to create a list for each column one list which correspond to the first element of each row mapping to current and the second element maps to the temperature.

As shown below. any better way to make the code look fancier?

-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)

To avoid doing the split call twice for every line, I'd suggest the following solution

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

This is a simple version that would be IMO reasonable up to a few megabytes of log file (it doesn't try to minimize memory usage or computing time during parsing):

 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])

Using generator expressions:

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)]

Warning, this isn't necessarily better as it's probably harder to read and understand what's going on. Be sure to document it properly with a docstring.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM