Skip to content

Instantly share code, notes, and snippets.

@zarino
Last active August 29, 2015 14:06
Show Gist options
  • Save zarino/c597d442560a513ea9cf to your computer and use it in GitHub Desktop.
Save zarino/c597d442560a513ea9cf to your computer and use it in GitHub Desktop.
Convert GNU Mailman output to tab-separated file for MailChimp
#!/usr/bin/env python
# Assumes a file called mailman.txt in the working directory,
# generated by: `list_members -p -f -n enabled -o ~/mailman.txt news`
import csv
import re
with open('exported.tsv', 'wb') as csvfile:
csvwriter = csv.writer(csvfile, delimiter="\t")
with open('mailman.txt', 'r') as f:
for line in f:
stripped = line.strip()
matches = re.match(r'^"*([^"]+)"* <(.+)>$', stripped)
if matches:
email = matches.group(2)
name = matches.group(1)
name = re.sub(r'\\([()])', '\g<1>', name)
csvwriter.writerow([email, name])
else:
csvwriter.writerow([stripped])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment