Created
May 30, 2011 21:13
-
-
Save I82Much/999481 to your computer and use it in GitHub Desktop.
Python checkmark
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
import sys | |
from collections import defaultdict | |
import csv | |
# see http://stackoverflow.com/questions/6180609/group-and-check-mark-using-python | |
def main(): | |
# files = ["group.txt"] | |
files = sys.argv[1:] | |
if len(files) < 1: | |
print "usage: ./python_checkmark.py file1 [file2 ... filen]" | |
name_map = defaultdict(set) | |
for f in files: | |
file_handle = open(f, "r") | |
process_file(file_handle, name_map) | |
file_handle.close() | |
print_csv(sys.stdout, name_map) | |
def process_file(input_file, name_map): | |
cur_name = "" | |
for line in input_file: | |
if ":" in line: | |
cur_name, item = [x.strip() for x in line.split(":")] | |
else: | |
item = line.strip() | |
name_map[cur_name].add(item) | |
def print_csv(output_file, name_map): | |
names = name_map.keys() | |
items = set([]) | |
for item_set in name_map.values(): | |
items = items.union(item_set) | |
writer = csv.writer(output_file, quoting=csv.QUOTE_MINIMAL) | |
writer.writerow( [""] + names ) | |
for item in sorted(items): | |
row_contents = map(lambda name:"X" if item in name_map[name] else "", names) | |
row = [item] + row_contents | |
writer.writerow( row ) | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment