Created
January 25, 2017 02:31
-
-
Save MitMaro/b0e55a912660a4d89c60cf12e0266dd1 to your computer and use it in GitHub Desktop.
BOM parsing and writing
This file contains 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 csv | |
connectors = {} | |
REF = 0 | |
FOOTPRINT = 1 | |
LABEL = 2 | |
lines = [] | |
with open('my-bom.bom', newline='') as f: | |
reader = csv.reader(f, delimiter=';') | |
for row in reader: | |
if row[REF].startswith('P'): | |
if ! connectors[row[FOOTPRINT]]: | |
connectors[row[FOOTPRINT]] = [] | |
connectors[row[FOOTPRINT]].append( | |
{ | |
ref: row[REF], | |
label: row[LABEL] | |
} | |
) | |
else: | |
lines.append(row) | |
# connectors: | |
# 02x02: [ | |
# {ref: P1, label: LED}, | |
# {ref: P2, label: Power In} | |
# ] | |
for footprint in connectors: | |
lines.append([ | |
', '.join(map((lambda x: x.ref), connectors[footprint])), | |
footprint, | |
', '.join(map((lambda x: x.label), connectors[footprint])), | |
len(connectors[footprint]) | |
]) | |
with open('my-new-bom.bom', newline='') as f: | |
writer = csv.writer(csvfile, delimiter=';') | |
for r in lines: | |
writer.writerow(r) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment