Skip to content

Instantly share code, notes, and snippets.

@frafra
Created October 8, 2017 19:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save frafra/7c9b5de37e6680b4de4cfdd7703d62a6 to your computer and use it in GitHub Desktop.
Save frafra/7c9b5de37e6680b4de4cfdd7703d62a6 to your computer and use it in GitHub Desktop.
posta2csv.py
#!/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