Created
April 11, 2015 14:35
-
-
Save vmassuchetto/6c445b37647c02fcd0c5 to your computer and use it in GitHub Desktop.
SolidWorks BOM to database parent-child part number conversion
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
""" | |
Python script to generate SolidWorks BOM lists into a database-usable parent | |
child relationship. | |
CSV data of the first column: | |
"<parent>.<child>.<subchild>.<subsubchild>" | |
Usage: | |
python sw-struct.py input.csv output.csv | |
""" | |
import sys | |
import csv | |
with open(sys.argv[1], 'r') as csvfile: | |
data = {} | |
tree = {} | |
for row in csv.reader(csvfile, delimiter=',', quotechar='"'): | |
_id = row[0].strip() | |
_parent = row[0].split('.') | |
_parent = '.'.join(_parent[0:len(_parent)-1]).strip() | |
_parent = _parent if _parent else 'ROOT' | |
tree.setdefault(_parent, []).append(_id) | |
data.setdefault(_parent, row) | |
data.setdefault(_id, row) | |
out = csv.writer(open(sys.argv[2], 'w'), delimiter=',', quoting=csv.QUOTE_ALL) | |
for _parent in tree: | |
for _id in tree[_parent]: | |
out.writerow([data[_parent][1]] + data[_id]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment