Commit 1e8430d5 authored by Dick Hollenbeck's avatar Dick Hollenbeck

Lorenzo's help enabled a fix to UTF8 support in csv.writer in python bom generators.

parent c7531d6c
...@@ -27,16 +27,23 @@ except IOError: ...@@ -27,16 +27,23 @@ except IOError:
# are created a tab delimited list instead! # are created a tab delimited list instead!
out = csv.writer(f, lineterminator='\n', delimiter='\t', quoting=csv.QUOTE_NONE) out = csv.writer(f, lineterminator='\n', delimiter='\t', quoting=csv.QUOTE_NONE)
# override csv.writer's writerow() to support utf8 encoding:
def writerow( acsvwriter, columns ):
utf8row = []
for col in columns:
utf8row.append( str(col).encode('utf8') )
acsvwriter.writerow( utf8row )
# Output a field delimited header line # Output a field delimited header line
out.writerow(['Source:', net.getSource()]) writerow( out, ['Source:', net.getSource()] )
out.writerow(['Date:', net.getDate()]) writerow( out, ['Date:', net.getDate()] )
out.writerow(['Tool:', net.getTool()]) writerow( out, ['Tool:', net.getTool()] )
out.writerow(['Component Count:', len(net.components)]) writerow( out, ['Component Count:', len(net.components)] )
out.writerow(['Ref', 'Value', 'Part', 'Documentation', 'Description', 'Vendor']) writerow( out, ['Ref', 'Value', 'Part', 'Documentation', 'Description', 'Vendor'] )
components = net.getInterestingComponents() components = net.getInterestingComponents()
# Output all of the component information # Output all of the component information
for c in components: for c in components:
out.writerow([c.getRef(), c.getValue(), c.getLibName() + ":" + c.getPartName(), writerow( out, [c.getRef(), c.getValue(), c.getLibName() + ":" + c.getPartName(),
c.getDatasheet(), c.getDescription(), c.getField("Vendor")]) c.getDatasheet(), c.getDescription(), c.getField("Vendor")])
...@@ -26,17 +26,24 @@ except IOError: ...@@ -26,17 +26,24 @@ except IOError:
# Create a new csv writer object to use as the output formatter # Create a new csv writer object to use as the output formatter
out = csv.writer(f, lineterminator='\n', delimiter=',', quotechar="\"", quoting=csv.QUOTE_ALL) out = csv.writer(f, lineterminator='\n', delimiter=',', quotechar="\"", quoting=csv.QUOTE_ALL)
# override csv.writer's writerow() to support utf8 encoding:
def writerow( acsvwriter, columns ):
utf8row = []
for col in columns:
utf8row.append( str(col).encode('utf8') )
acsvwriter.writerow( utf8row )
# Output a field delimited header line # Output a field delimited header line
out.writerow(['Source:', net.getSource()]) writerow( out, ['Source:', net.getSource()] )
out.writerow(['Date:', net.getDate()]) writerow( out, ['Date:', net.getDate()] )
out.writerow(['Tool:', net.getTool()]) writerow( out, ['Tool:', net.getTool()] )
out.writerow(['Component Count:', len(net.components)]) writerow( out, ['Component Count:', len(net.components)] )
out.writerow(['Ref', 'Value', 'Footprint', 'Datasheet', 'Manufacturer', 'Vendor']) writerow( out, ['Ref', 'Value', 'Footprint', 'Datasheet', 'Manufacturer', 'Vendor'] )
components = net.getInterestingComponents() components = net.getInterestingComponents()
# Output all of the component information (One component per row) # Output all of the component information (One component per row)
for c in components: for c in components:
out.writerow([c.getRef(), c.getValue(), c.getFootprint(), c.getDatasheet(), writerow( out, [c.getRef(), c.getValue(), c.getFootprint(), c.getDatasheet(),
c.getField("Manufacturer"), c.getField("Vendor")]) c.getField("Manufacturer"), c.getField("Vendor")])
...@@ -47,15 +47,22 @@ columns = ['Item', 'Qty', 'Reference(s)', 'Value', 'LibPart', 'Footprint', 'Data ...@@ -47,15 +47,22 @@ columns = ['Item', 'Qty', 'Reference(s)', 'Value', 'LibPart', 'Footprint', 'Data
# Create a new csv writer object to use as the output formatter # Create a new csv writer object to use as the output formatter
out = csv.writer(f, lineterminator='\n', delimiter=',', quotechar='\"', quoting=csv.QUOTE_MINIMAL) out = csv.writer(f, lineterminator='\n', delimiter=',', quotechar='\"', quoting=csv.QUOTE_MINIMAL)
# override csv.writer's writerow() to support utf8 encoding:
def writerow( acsvwriter, columns ):
utf8row = []
for col in columns:
utf8row.append( str(col).encode('utf8') )
acsvwriter.writerow( utf8row )
# Output a set of rows as a header providing general information # Output a set of rows as a header providing general information
out.writerow(['Source:', net.getSource()]) writerow( out, ['Source:', net.getSource()] )
out.writerow(['Date:', net.getDate()]) writerow( out, ['Date:', net.getDate()] )
out.writerow(['Tool:', net.getTool()]) writerow( out, ['Tool:', net.getTool()] )
out.writerow(['Component Count:', len(components)]) writerow( out, ['Component Count:', len(components)] )
out.writerow([]) writerow( out, [] )
out.writerow(['Individual Components:']) writerow( out, ['Individual Components:'] )
out.writerow([]) # blank line writerow( out, [] ) # blank line
out.writerow(columns) writerow( out, columns )
# Output all the interesting components individually first: # Output all the interesting components individually first:
row = [] row = []
...@@ -74,16 +81,16 @@ for c in components: ...@@ -74,16 +81,16 @@ for c in components:
for field in columns[7:]: for field in columns[7:]:
row.append( c.getField( field ) ); row.append( c.getField( field ) );
out.writerow(row) writerow( out, row )
out.writerow([]) # blank line writerow( out, [] ) # blank line
out.writerow([]) # blank line writerow( out, [] ) # blank line
out.writerow([]) # blank line writerow( out, [] ) # blank line
out.writerow(['Collated Components:']) writerow( out, ['Collated Components:'] )
out.writerow([]) # blank line writerow( out, [] ) # blank line
out.writerow(columns) # reuse same columns writerow( out, columns ) # reuse same columns
...@@ -121,6 +128,6 @@ for group in grouped: ...@@ -121,6 +128,6 @@ for group in grouped:
for field in columns[7:]: for field in columns[7:]:
row.append( net.getGroupField(group, field) ); row.append( net.getGroupField(group, field) );
out.writerow( row ) writerow( out, row )
f.close() f.close()
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment