Last active
August 29, 2015 14:06
-
-
Save zarino/c597d442560a513ea9cf to your computer and use it in GitHub Desktop.
Convert GNU Mailman output to tab-separated file for MailChimp
This file contains hidden or 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 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