Created
October 8, 2017 19:22
-
-
Save frafra/7c9b5de37e6680b4de4cfdd7703d62a6 to your computer and use it in GitHub Desktop.
posta2csv.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
# | |
# Convertitore delle mail del Raspberry Pi di Ugone | |
# Scritto da Frafra (2017) | |
import collections | |
import csv | |
import glob | |
messages = glob.glob('messaggi/*.txt') | |
output = 'output.csv' | |
keys = [ | |
"Data", | |
"Humidity", | |
"Pressure", | |
"Temperature", | |
] | |
with open(output, 'w', newline='') as csvfile: | |
csvwriter = csv.writer(csvfile) | |
csvwriter.writerow(keys) | |
for message in messages: | |
with open(message) as mail: | |
result = collections.defaultdict(str) | |
for line in mail.readlines(): | |
while ': ' in line: | |
key, value = line.rsplit(': ', 1) | |
key, value = key.strip(), value.strip() | |
if ' ' in key: | |
line, key = key.rsplit(' ', 1) | |
else: | |
line = '' | |
result[key] = value | |
csvwriter.writerow([result[key] for key in keys]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment