Update: The Solution
I managed to get the following code to work
import collections
from lxml import etree
## Up here is code for getting an .xml input file from the user, opening that file, etc. ##
## This part is in a for loop that goes over each order in the xml file ##
## This all would have an extra indent because it is under this: for order in root.xpath('//order'): ##
itemlist = []
## This part looks through the .xml file for the order it is currently iterating and puts the items into a list ##
for element in order.iter('items'):
itemlist.append ("%s" % str.upper((element.get('type'))))
## This part 'sanitizes' the order name from the .xml file for use as a key ##
for element in order.iter('order'):
ordername = element.get('name')
strippedordername = re.sub('[/\()!@#$%^&*()]', '', ordername)
allordernames.append (strippedordername)
print strippedordername
#print itemlist
## This bit compiles a shopping list of items in a special dict subclass called a Counter. ##
ordercounter.update(itemlist)
## This part makes a dict with order names for its keys and their corresponding Counter of items as its values ##
ordersdictsdict[strippedordername] = collections.Counter(itemlist)
zeros = dict((k,0) for k in ordercounter.keys())
for cntr in ordersdictsdict.values():
cntr.update(zeros)
#print ordercounter
#print ordersdictsdict
key_order = list(ordercounter.keys())
print key_order
with open(out_file,'w') as fout:
fout.write('Order,'+','.join(key_order)+'\n')
fout.write('Totals,'+','.join(str(ordercounter[k]) for k in key_order)+'\n')
for ordername,dct in ordersdictsdict.items():
fout.write(ordername+','+','.join(str(dct[k]) for k in key_order)+'\n')
fout.closed
The output ends up looking like this:
Order,Spam,Eggs,Baked Beans,Sausage
Totals,13,1,1,1
Order for Joe,2,1,0,1
Order for Jill,11,0,1,0
What I Have
My script takes an input xml file and parses it, looking for order name and then order contents. There can be multiple orders in one xml file. I then have a counter that tallies up all the items from all the orders and gives me a grand total shopping list.
Given these two sample orders:
Order for Joe: Spam, Egg, Sausage, Spam
Order for Jill: Spam, Spam, Spam, Spam, Spam, Spam, Spam, Beaked Beans, Spam, Spam, Spam, Spam
The counter would look like this:
Counter({'Spam': 13,'Baked Beans' 1, 'Egg': 1, 'Sausage': 1})
I then write this to a csv file so that it looks like this:
Item,Count
Spam,13
Baked Bean,1
Egg,1
Sausage,1
What I Want
While the grand total shopping list is nice, I’d like to expand my output csv file to also include a shopping list for each order name. I don’t care if order names are the rows or the columns. I also don’t really care if the cell for an item not in that order is a 0 or empty but I’ll use 0 in my examples.
Example Desired Output with Order Names as Rows
Order Name,Spam,Baked Beans,Egg,Sausage
Totals,13,1,1,1
Order for Joe,2,0,1,1
Order for Jill,11,1,0,0
Example Desired Output with Order Names as Columns
Item,Totals,Order for Joe,Order for Jill
Spam,13,2,11
Baked Beans,1,0,1
Egg,1,1,0
Sausage,1,1,0
Notes
I want this script to work on any input file – of course if the input only contains one order, then Totals will match that order name. I have to first make a grand total counter (so that I have all the possible items for the order(s) in question) and then fill in the csv with counts from each order. In other words, I can’t start my csv file by writing the items to it hard coded because the next input file might have different items in the orders.
Why can’t you use a
Counterfor every line of the input file?You may need to get a little more tricky to deal with quoting if your items can have commas in the names (Think
csvmodule for that stuff), but this should give you a good place to start.