In [25]:
import csv
import os.path

In [26]:
# Select input files
inputFilePrefix = 'BOM'
inputs = [
    ['smartsensor.csv', 180+70+60+30+30],
    ['smartsensorPowered_servo.csv', 30],
    ['battery_buzzer.csv', 70],
    ['metal_detector_circuit.csv', 30],
    ['team_flag.csv', 60],
    ['cape.csv', 70],
    ['arduino_smartsensor_shield.csv', 0],
    ['grizzly_bear.csv', 285],
]
# Select output files
outputCsv = 'totalBom.csv'
paramOut = 'boardCount.txt'

# Sorting config
sortKeys = ['Value','Package','Manufacturer Part Number',
            'Distributor Part Number']
orderKey = 'Reference Designator'  # Try to order the output in a nice way
orderKey2 = 'Value'
180+70+60+30+30


Out[26]:
370

In [27]:
allRows = []  # first column is the part count
tableHeader = None
sortKeyIndexes = None

for boardInfo in inputs:
    fname = os.path.join(inputFilePrefix, boardInfo[0])
    boardMult = boardInfo[1]
    with open(fname, 'rb') as csvFile:
        csvReader = csv.reader(csvFile, delimiter=',')
        firstRow = True
        for row in csvReader:
            if firstRow:
                if tableHeader is None:
                    tableHeader = ['Count']+row
                firstRow = False
            else:
                allRows.append([boardMult] + row)

In [28]:
sortKeyIndexes = [tableHeader.index(key) for key in sortKeys]
orderKeyIndex = tableHeader.index(orderKey)
orderKey2Index = tableHeader.index(orderKey2)
allRows2 = []
for row in allRows:
    keys = [row[i] for i in sortKeyIndexes]
    key = ' : '.join(keys)
    allRows2.append(row + [key])

In [29]:
partDict = {}  # {key: (total count, row)}
for row in allRows2:
    key = row[-1].upper()
    if partDict.has_key(key):
        partDict[key][0] += row[0]
    else:
        partDict[key] = [row[0], row[1:-1]]

In [30]:
totalRows = []
for key, value in partDict.iteritems():
    totalRows.append([value[0]] + value[1])

In [31]:
totalRows.sort(key=lambda row:(''.join([i for i in row[orderKeyIndex] if not i.isdigit()])+
                               ' '+''.join(row[orderKey2Index].split()))
                              .upper())

In [32]:
# Write output CSV
fname = os.path.join(inputFilePrefix, outputCsv)
with open(fname, 'wb') as csvFile:
    csvWriter = csv.writer(csvFile, delimiter=',')
    csvWriter.writerow(tableHeader)
    for row in totalRows:
        if row[0] != 0:
            csvWriter.writerow(row)

In [33]:
# Write output params
fname = os.path.join(inputFilePrefix, paramOut)
with open(fname, 'w') as paramFile:
    paramFile.write("Board count:\n")
    for boardConfig in inputs:
        string = '%d x %s\n' % (boardConfig[1], boardConfig[0])
        paramFile.write(string)

In [ ]:


In [34]:
''.join([i for i in "JP123" if not i.isdigit()])


Out[34]:
'JP'

In [13]:
findStr = "47uF"
for boardInfo in inputs:
    fname = os.path.join(inputFilePrefix, boardInfo[0])
    boardMult = boardInfo[1]
    with open(fname, 'rb') as csvFile:
        csvReader = csv.reader(csvFile, delimiter=',')
        for row in csvReader:
            string = ','.join(row)
            if findStr in string:
                print boardInfo[0], ':', string


smartsensorPowered_servo.csv : COUT1,47uF,1206,Capacitor,TDK Corporation,C3216X5R1C476M160AB,DigiKey,445-6003-2-ND,at least 16V, prefer X5R/X7R (avoid Y5V)
smartsensorPowered_servo.csv : CSS,.47uF,0603-CAP,Capacitor,Samsung Electro-Mechanics America, Inc,CL10B474KO8NNNC,DigiKey,1276-1062-2-ND,at least 16V, prefer X5R/X7R (avoid Y5V)

In [ ]: