1
我有一個文本文件,我試圖解析並重新格式化爲.csv文件。該文本文件被創建爲每2Hz重複的狀態消息,輸出各種值。每封郵件都有'-----------'標題,'/ n'換行符和''頁腳。解析重複郵件的最佳Pythonic方法
我認爲最好的辦法是一次只讀一個'消息',並且能夠隨時更新輸出文件;我是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
我要拉出來像每一個消息,溫度,電壓的日期/時間的事情,等
這裏是我的超級非Python的方式(請注意,複製和粘貼在壓痕弄亂...這是正確的真正的代碼):
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()
re模塊如何比我寫的更好?它快嗎?更容易閱讀? – drone007