用于解析重复消息的最佳Pythonic方法

时间:2016-10-25 14:19:38

标签: python csv parsing message repeat

我有一个文本文件,我试图解析并重新格式化为.csv。文本文件每2Hz创建为重复状态消息,输出各种值。每封邮件都有一个'-----------'标题,'/ n'换行符和一个'页脚。

我认为最好的方法是一次只读一个'消息',并且能够随时更新我的​​输出文件;我是Python的新手(使用Python 3.5),我觉得我提出的所有内容都是更加C ++风格的。

输入数据如下所示:

    -----------
2016-10-10T14:03:38,158000277+0000
 14:03:38 up 1 min,  2 users,  load average: 0.58, 0.13, 0.04
acpitz-virtual-0
Adapter: Virtual device
temp1:        +27.8°C  (crit = +110.0°C)
temp2:        +29.8°C  (crit = +110.0°C)

coretemp-isa-0000
Adapter: ISA adapter
Physical id 0:  +44.0°C  (high = +105.0°C, crit = +105.0°C)
Core 0:         +44.0°C  (high = +105.0°C, crit = +105.0°C)
Core 1:         +44.0°C  (high = +105.0°C, crit = +105.0°C)

nct6776-isa-0a00
Adapter: ISA adapter
Vcore:                  +1.79 V  (min =  +0.00 V, max =  +1.74 V)  ALARM
in1:                    +1.35 V  (min =  +0.00 V, max =  +0.00 V)  ALARM
AVCC:                   +3.33 V  (min =  +2.98 V, max =  +3.63 V)
+3.3V:                  +3.33 V  (min =  +2.98 V, max =  +3.63 V)
in4:                    +1.00 V  (min =  +0.00 V, max =  +0.00 V)  ALARM
in5:                    +0.00 V  (min =  +0.00 V, max =  +0.00 V)
in6:                    +0.27 V  (min =  +0.00 V, max =  +0.00 V)  ALARM
3VSB:                   +3.30 V  (min =  +2.98 V, max =  +3.63 V)
Vbat:                   +3.20 V  (min =  +2.70 V, max =  +3.63 V)
fan1:                     0 RPM  (min =    0 RPM)
fan2:                     0 RPM  (min =    0 RPM)
SYSTIN:                 +68.0°C  (high =  +0.0°C, hyst =  +0.0°C)  ALARM  sensor = thermistor
CPUTIN:                 +22.5°C  (high = +80.0°C, hyst = +75.0°C)  sensor = CPU diode
AUXTIN:                 +28.5°C  (high = +80.0°C, hyst = +75.0°C)  sensor = CPU diode
PCH_CHIP_CPU_MAX_TEMP:  +26.0°C  (high = +80.0°C, hyst = +75.0°C)
PECI Agent 0:           +44.0°C  (high = +80.0°C, hyst = +75.0°C)
                                 (crit = +105.0°C)
PCH_CHIP_TEMP:           +0.0°C  
PCH_CPU_TEMP:            +0.0°C  
intrusion0:            OK
intrusion1:            OK

我想提取每条信息的日期/时间,温度,电压等信息。

这是我的超级非Pythonic方法(请注意,在复制和粘贴期间,缩进变得混乱......在实际代码中它是正确的):

with open('102_sensors.log', 'r') as readFile, open('output.txt','w') as writeFile:
for line in readFile:
    if line.startswith('2016'):            
        writeFile.write(line[:10] + ',' + line[11:-1] + ',')    #date and time
    if 'load average' in line:
        writeFile.write(line[13:15] + ',' + line[-17:-1] + ',') #up-time and load avgs
    if 'temp1' in line:
        writeFile.write(line[15:19] + ',')             
    if 'temp2' in line:   
        writeFile.write(line[15:19] + ',')              
    if 'Physical id 0' in line:   
        writeFile.write(line[17:21] + ',')
    if 'Core 0' in line:   
        writeFile.write(line[17:21] + ',')
    if 'Core 1' in line:   
        writeFile.write(line[17:21] + ',')
    if 'Vcore' in line:   
        writeFile.write(line[25:29] + ',')
    if 'in1' in line:   
        writeFile.write(line[25:29] + ',')
    if 'AVCC' in line:   
        writeFile.write(line[25:29] + ',')
    if 'threeV' in line:   
        writeFile.write(line[25:29] + ',')
    if 'in4' in line:   
        writeFile.write(line[25:29] + ',')
    if 'in5' in line:   
        writeFile.write(line[25:29] + ',')
    if 'in6' in line:   
        writeFile.write(line[25:29] + ',')
    if 'threeVSB' in line:   
        writeFile.write(line[25:29] + ',')
    if 'Vbat' in line:   
        writeFile.write(line[25:29] + ',')
    if 'SYSTIN' in line:   
        writeFile.write(line[25:29] + ',')
    if 'CPUTIN' in line:   
        writeFile.write(line[25:29] + ',')
    if 'AUXTIN' in line:   
        writeFile.write(line[25:29] + ',')
    if '-----------' in line:
        writeFile.write('\n')
        continue
writeFile.close()

1 个答案:

答案 0 :(得分:0)

使用re模块解析输入。使用csv模块写入输出。以下是reference manual

的示例
import csv
with open('eggs.csv', 'w', newline='') as csvfile:
    spamwriter = csv.writer(csvfile, delimiter=' ',
                            quotechar='|', quoting=csv.QUOTE_MINIMAL)
    spamwriter.writerow(['Spam'] * 5 + ['Baked Beans'])
    spamwriter.writerow(['Spam', 'Lovely Spam', 'Wonderful Spam'])