Parse contacts from email in python

Process all your email contacts using mu and python.

mu-cfind returns all contacts:

mu cfind

Excerpt from the returned results

...
Newsletter Museum Tinguely infos@tinguely.ch
...

Script

import subprocess
from collections import namedtuple

# fetch raw input
query = "mu cfind"
subprocess.check_output((query), shell=True).splitlines()
runQuery = subprocess.check_output((query), shell=True).splitlines()
raw_contacts = [line.decode("utf-8").split(" ") for line in runQuery]

# process input to output
Contact = namedtuple('Contact', ['email', 'name'])

Contacts = tuple(
# Get email including only the last element
    Contact(email=raw_contact[-1],
# Get name from all elements excluding the last element
            name=raw_contact[:-1])
    for raw_contact in raw_contacts)

# print to stdout tab separated
for Contact in Contacts:
    print(Contact.email," ".join(Contact.name),sep="\t")

</tmp/mu_contacts.tsv>

grep "Tinguely" /tmp/mu_contacts.tsv
infos@tinguely.ch	Newsletter Museum Tinguely