Calculate, sort and modify components

My import list is a list with various components. Scripts count how many such components are.
I try to sort them and put a "part" in front of them.

code:

Using code from @MagunRa

      

Input file (new):

Part                    Value          Package               Library                                Position (mm)         Orientation
C1       100n                              C0402             rcl              (117.65 40.75)        R270
C2       100n                              C0603             rcl              (-48.26 53.34)        R0
C3       1u                                C0402             rcl              (-50.8 78.74)         R0
C4       1u                                C0402             rcl              (9 63.1)              R270
C5       10n-1%                            C0402             rcl              (81.55 39.65)         R270
C6       100n                              C0603             rcl              (-54.61 53.34)        R0
C7       100n                              C0402             rcl              (-60.96 53.34)        R0
C8       100n                              C0603             rcl              (-67.31 53.34)        R0
C9       100n                              C0603             rcl              (-73.66 53.34)        R0
C10      100n                              C0402             rcl              (117.8 50.55)         R270
C11      100n                              C0603             rcl              (-17.78 58.42)        R0
C12      100n                              C0603             rcl              (-24.13 58.42)        R0
C13      1u                                C0402             rcl              (80.35 45.95)         R0
C14      100n                              C0402             rcl              (78.15 52.75)         R0
C15      1u                                C0402             rcl              (81.5 44.1)           R90
C16      10u                               C0402             rcl              (79.25 40.75)         R270
C17      22u                               C0603             rcl              (-59.69 63.5)         R0
C18      100n                              C0603             rcl              (-36.83 58.42)        R0
C19      100n                              C0603             rcl              (-43.18 58.42)        R0
C20      100n                              C0603             rcl              (-49.53 58.42)        R0
C21      100u                              A/3216-18R        rcl              (-60.96 40.64)        R0
C22      100n                              C0402             rcl              (-55.88 58.42)        R0
C23      10n                               C0402             rcl              (-64.77 63.5)         R0
C24      22u                               C0603             rcl              (-69.85 63.5)         R0
C25      100n                              C0402             rcl              (-62.23 58.42)        R0
C26      10n                               C0402             rcl              (-74.93 63.5)         R0
C27      100n                              C0402             rcl              (83.15 51.4)          R0
C28      1u                                C0402             rcl              (78.3 50.25)          R180
C29      47uF                              C0603             rcl              (-35.56 53.34)        R0
C30      0.5p                              C0402             rcl              (83.8 46.35)          MR270
C31      100n                              C0603             rcl              (-74.93 58.42)        R0
C32      100n                              C0603             rcl              (-17.78 63.5)         R0
C33      10u                               C0402             rcl              (-16.51 68.58)        R0
C34      120n                              C0603             rcl              (-24.13 63.5)         R0
C34_X5R  >2.2u                             C0402             rcl              (-21.59 53.34)        R0
C34_X5R1 >2.2u                             C0402             rcl              (-80.01 48.26)        R0
C35      10u                               C0402             rcl              (-21.59 68.58)        R0
C36      1u                                C0402             rcl              (83.15 52.45)         R180
C37      6.8n                              C0603             rcl              (-34.29 63.5)         R0
C40      10u                               C0402             rcl              (83.1 42.85)          MR180
C53      1u                                C0603             rcl              (-36.83 68.58)        R0
C54      12p                               C0603             rcl              (-41.91 68.58)        R0
C55      12p                               C0603             rcl              (-46.99 68.58)        R0
C56      100u                              A/3216-18R        rcl              (-66.04 40.64)        R0
C57      100u                              A/3216-18R        rcl              (-71.12 40.64)        R0
C58      100u                              A/3216-18R        rcl              (-76.2 40.64)         R0
D1       PD3S160                           POWERDI323        diode            (-46.99 41.91)        R0
D2       PD3S160                           POWERDI323        diode            (-55.88 41.91)        R0
IC1      OPA333DBV                         SOT23-5           linear           (79.2 48.1)           R90
J1                                         MOLEX_47346-0001  Molex-47346-0001 (-39.37 17.78)        R0
L3       BLM15HD182SN1                     0402              inductors        (-53.34 48.26)        R0
L4       BLM15HD182SN1                     0402              inductors        (-69.85 48.26)        R0
R1       33M                               R0603             rcl              (82.65 46.3)          MR90
R2       10K                               R0402             rcl              (118.85 50.55)        R270
R3       2k2                               R0402             rcl              (-31.75 78.74)        R0
R4       10K                               R0402             rcl              (118.85 40.75)        R270
R5       10k                               R0402             rcl              (-36.83 78.74)        R0
R6       10k                               R0402             rcl              (83.1 43.9)           MR0
R7       10k                               R0603             resistor         (-36.83 48.26)        R0
R8       2k2                               R0402             rcl              (106.4 40.8)          R90
R9       220                               R0603             rcl              (-26.67 78.74)        R0
R10      100                               R0402             rcl              (107.85 43.95)        R90
R11      2k2                               R0402             rcl              (106.4 50.65)         R270
R12      100                               R0402             rcl              (107.85 47.45)        R270
R13      4.7                               R0402             rcl              (81.55 41.65)         R270
R14      47                                R0402             rcl              (115.2 40.75)         R90
R15      2k2                               R0402             rcl              (110.85 40.75)        R270
R16      47                                R0402             rcl              (115.2 50.55)         R270
R17      10k                               R0603             resistor         (-16.51 48.26)        R0
R18      15                                R0603             rcl              (122.15 43.85)        R90
R19      2k2                               R0402             rcl              (110.85 50.55)        R90
R20      10                                R0603             rcl              (122.15 47.5)         R270
R21      10k                               R0603             resistor         (-21.59 48.26)        R0
R22      4.7                               R0402             rcl              (79.95 39.1)          R0
R23      47k                               R0402             rcl              (78.35 51.55)         R180
R24      1k                                R0402             rcl              (77.85 54.05)         R0
R27      1k                                R0402             rcl              (82.6 45.4)           R90
R28      20k                               R0402             rcl              (78.2 45.95)          R0
R29      0.2                               R0603             rcl              (80.4 41.1)           R90
R35      10K                               R0402             rcl              (80.45 50.25)         R0
R36      10k                               R0603             resistor         (-26.67 48.26)        R0
R42      500                               R0603             resistor         (-31.75 48.26)        R0
R48      100                               R0402             rcl              (116.45 40.75)        R90
R49      100                               R0402             rcl              (116.75 50.55)        R90
SOT23-1  LTC6240                           SOT23-5           artinis          (82.3 50.2)           MR180
T1       PMV16UN                           SOT-23            zetex            (121 40.75)           R90
T2       FMMT617                           SOT-23            zetex            (108.6 40.85)         R180
T3       FMMT717                           SOT-23            zetex            (-40.64 25.4)         R0
T4       FMMT617                           SOT-23            zetex            (108.6 50.55)         R0
T5       PMV16UN                           SOT-23            zetex            (121 50.55)           R90
T6       FMMT617                           SOT-23            zetex            (113 40.65)           R0
T8       FMMT617                           SOT-23            zetex            (113.05 50.65)        R180
U$1      BPW34SMD                          BPW_34_SMD        jwasys           (80 45.7)             MR180
U$2      TPS73433                          SOT23-5           artinis          (-48.26 15.24)        R0
U$4      LED760/850_ROITHNER               DUOLED_5MM        jwasys           (110 45.7)            R90
U$5      LED760/850_ROITHNER               DUOLED_5MM        jwasys           (115 45.7)            R90
U$6      LED760/850_ROITHNER               DUOLED_5MM        jwasys           (120 45.7)            R90
U$7      TM4C123GH6ZRBBGA157               BGA_157_MICROSTAR artinis          (-26.67 6.35)         R0
U$8      REF3225                           SOT95P280X100-6N  artinis          (82.95 48.95)         R90
U$10     TPS73633                          SOT23-5           artinis          (-55.88 15.24)        R0
U$12     BNO055LGA-28                      LGA-28            artinis          (-59.69 78.74)        R0
U$14     ECX-42_CRYSTALECS-160-9-42-CKM-TR ECX-42_CRYSTAL    artinis          (-67.31 77.47)        R0
U$16     HEADER_1.27_7                     HEADER_1.27_7     jwasys           (-73.66 77.47)        R0
U1       ADS8860                           SOP50P490X110-10N artinis          (85.3 40.35)          R0
U6       THS4281DBVT                       SOT95P280X145-5N  artinis          (78.85 43.85)         R180

      

Output from code (MagunRa):

Part - Value - Package - Amount
['R29'],0.2,R0603,1
['C30'],0.5p,C0402,1
['R20'],10,R0603,1
['R10', 'R12', 'R48', 'R49'],100,R0402,4
['C1', 'C2', 'C6', 'C7', 'C8', 'C9', 'C10', 'C11', 'C12', 'C14', 'C18', 'C19', 'C20', 'C22', 'C25', 'C27', 'C31', 'C32'],100n,C0402,7
['C1', 'C2', 'C6', 'C7', 'C8', 'C9', 'C10', 'C11', 'C12', 'C14', 'C18', 'C19', 'C20', 'C22', 'C25', 'C27', 'C31', 'C32'],100n,C0603,11
['C21', 'C56', 'C57', 'C58'],100u,A/3216-18R,4
['R2', 'R4', 'R35'],10K,R0402,3
['R5', 'R6', 'R7', 'R17', 'R21', 'R36'],10k,R0402,2
['R5', 'R6', 'R7', 'R17', 'R21', 'R36'],10k,R0603,4
['C23', 'C26'],10n,C0402,2
['C5'],10n-1%,C0402,1
['C16', 'C33', 'C35', 'C40'],10u,C0402,4
['C34'],120n,C0603,1
['C54', 'C55'],12p,C0603,2
['R18'],15,R0603,1
['R24', 'R27'],1k,R0402,2
['C3', 'C4', 'C13', 'C15', 'C28', 'C36', 'C53'],1u,C0402,6
['C3', 'C4', 'C13', 'C15', 'C28', 'C36', 'C53'],1u,C0603,1
['R28'],20k,R0402,1
['R9'],220,R0603,1
['C17', 'C24'],22u,C0603,2
['R3', 'R8', 'R11', 'R15', 'R19'],2k2,R0402,5
['R1'],33M,R0603,1
['R13', 'R22'],4.7,R0402,2
['R14', 'R16'],47,R0402,2
['R23'],47k,R0402,1
['C29'],47uF,C0603,1
['R42'],500,R0603,1
['C37'],6.8n,C0603,1
['C34_X5R', 'C34_X5R1'],>2.2u,C0402,2
['U1'],ADS8860,SOP50P490X110-10N,1
['L3', 'L4'],BLM15HD182SN1,0402,2
['U$12'],BNO055LGA-28,LGA-28,1
['U$1'],BPW34SMD,BPW_34_SMD,1
['U$14'],ECX-42_CRYSTALECS-160-9-42-CKM-TR,ECX-42_CRYSTAL,1
['T2', 'T4', 'T6', 'T8'],FMMT617,SOT-23,4
['T3'],FMMT717,SOT-23,1
['U$16'],HEADER_1.27_7,HEADER_1.27_7,1
['U$4', 'U$5', 'U$6'],LED760/850_ROITHNER,DUOLED_5MM,3
['SOT23-1'],LTC6240,SOT23-5,1
['J1'],MOLEX_47346-0001,Molex-47346-0001,1
['IC1'],OPA333DBV,SOT23-5,1
['D1', 'D2'],PD3S160,POWERDI323,2
['T1', 'T5'],PMV16UN,SOT-23,2
['U$8'],REF3225,SOT95P280X100-6N,1
['U6'],THS4281DBVT,SOT95P280X145-5N,1
['U$7'],TM4C123GH6ZRBBGA157,BGA_157_MICROSTAR,1
['U$2'],TPS73433,SOT23-5,1
['U$10'],TPS73633,SOT23-5,1

      

The result I'm trying to get:

Part - Value - Package - Amount
[C1,C7,C10,C14,C22,C25,C27],100n,C0402,7
Etc,
Etc,

      

As you can see in the file, the part starts with a letter: C, D, R, L, etc.

Is it possible to sort so that it appears alphabetically?

+3


source to share


3 answers


The dict counter is not the best approach for your problem, if you want to associate a value with the first part you encounter, you can use the following approach which uses a tuple (Value, Package)

as a key and sets the part to the first part we encounter is related to pairing (Value, Package)

... Using islice is pointless as you are splitting the first three columns so there is no real benefit. Using OrderedDict will preserve the order of your file as well:

from collections import OrderedDict
import csv

d = OrderedDict()
with open('test.txt') as f, open("out.csv", "w") as out:
    wr = csv.DictWriter(out, fieldnames=["Part", "Value", 'Package', "Amount"])
    wr.writeheader()
    next(f) # skip header
    for line in f:
        a, b, c, _ = line.split(None, 3) # split into three columns
        key = (b, c) # Value and Package is our key
        d.setdefault(key, dict(Amount=0, Part=a, Value=b, Package=c)) 
        d[key]["Amount"] += 1 # increase count every time we see the key
    wr.writerows(d.itervalues()) # DictWriter will handle writing the rows

      

What are the outputs:



Part,Value,Package,Amount
C1,1nF,C0603,1
C2,100nF,C0603,8
C3,10uF_Tantalum,C0603,1
C4,22uF,C0603,1
C5,1uF,C0603,1
C13,0.22uF,C0603,1
C14,2.2uF,C0603,2
HDR_PRGRM_NIET_PLAATSEN,HEADER_1.27_6P,HEADER_1.27_6,1
L1,MURATA10UH,R1210,1
R1,22K,R0603,2
R3,100,R0603,4
R4,483,R0603,1
R10,10k,R0603,1
R11,10K,R0603,2
R14,19.6K,R0603,1
TP1,PTR1TP20R,TP20R,4
TP5,PTR1PAD1-13Y,P1-13Y,2
U$1,WML-C20,WML-C20,1
U$4,MCP4921-E/MS,SOP65P490X110-8N,1
U$6,TSV912DFN8_2X2,DFN8_2X2,1
U$9,LTC3525_BOOST,SC70-6,1
U1,REG71055DDCT,SOT95P180X110-6N,1
U2,ATMEGA32L-8MU,QFN50P700X700X100-45N,1
X1,53047-05,53047-05,1

      

If you want to keep all the parts and just add a score after each part, keep a list of all the parts associated with the pairing (Value, Package)

, not just the first one, and write each iteration of each part, value, package and amount v["Parts"]

that will contain all the parts associated with a particular (Value, Package)

to get the file in its original order, we need to use a sorting key, which will be the line at which the part appears in the file, using a dict to create a mapping from the order we saw the part:

from collections import defaultdict
import csv

d = defaultdict(lambda: dict(Amount=0, Parts=[], Value=b, Package=c))
with open('foo.txt') as f, open("out.csv", "w") as out:
    wr = csv.writer(out)
    order = {} # use this later to sort
    wr.writerow(["Part", "Value", 'Package', "Amount"])
    next(f)
    for ind, line in enumerate(f):
        a, b, c, _ = line.split(None, 3)
        order[a] =  ind
        key = (b, c)
        d[key]["Amount"] += 1
        d[key]["Parts"].append(a)
    # flatten the Parts so we can sort back to the original order
    flat = ((k,v["Amount"],v["Value"],v["Package"]) for v in d.itervalues() for k in
            v["Parts"])
    # sort by part
    wr.writerows(sorted(flat, key=lambda x: order[x[0]]))

      

What are the outputs:



Part,Value,Package,Amount
C1,C0603,1nF,1
C2,C0603,100nF,8
C3,C0603,10uF_Tantalum,1
C4,C0603,22uF,1
C5,C0603,1uF,1
C6,C0603,100nF,8
C7,C0603,100nF,8
C8,C0603,100nF,8
C11,C0603,100nF,8
C12,C0603,100nF,8
C13,C0603,0.22uF,1
C14,C0603,2.2uF,2
C15,C0603,2.2uF,2
C16,C0603,100nF,8
C17,C0603,100nF,8
HDR_PRGRM_NIET_PLAATSEN,HEADER_1.27_6,HEADER_1.27_6P,1
L1,R1210,MURATA10UH,1
R1,R0603,22K,2
R2,R0603,22K,2
R3,R0603,100,4
R4,R0603,483,1
R5,R0603,100,4
R7,R0603,100,4
R10,R0603,10k,1
R11,R0603,10K,2
R12,R0603,100,4
R13,R0603,10K,2
R14,R0603,19.6K,1
TP1,TP20R,PTR1TP20R,4
TP2,TP20R,PTR1TP20R,4
TP3,TP20R,PTR1TP20R,4
TP4,TP20R,PTR1TP20R,4
TP5,P1-13Y,PTR1PAD1-13Y,2
TP6,P1-13Y,PTR1PAD1-13Y,2
U$1,WML-C20,WML-C20,1
U$4,SOP65P490X110-8N,MCP4921-E/MS,1
U$6,DFN8_2X2,TSV912DFN8_2X2,1
U$9,SC70-6,LTC3525_BOOST,1
U1,SOT95P180X110-6N,REG71055DDCT,1
U2,QFN50P700X700X100-45N,ATMEGA32L-8MU,1
X1,53047-05,53047-05,1

      

I would recommend keeping a format like the above so you can read the files easily using the csv module.

To get the edited output, we only need to change the parts list and add all the parts:

from collections import OrderedDict
import csv
from itertools import islice

d = OrderedDict()
with open('test.csv') as f, open("out.csv", "w") as out:
    _ = list(islice(f, 10)) # skip 10 lines
    wr = csv.DictWriter(out, fieldnames=["Parts", "Value", 'Package', "Amount"])
    wr.writeheader()
    for line in f:
        a, b, c, _ = line.split(None, 3) # split into three columns
        key = (b, c) # Value and Package is our key
        d.setdefault(key, dict(Amount=0, Parts=[], Value=b, Package=c))
        d[key]["Amount"] += 1 # increase count every time we see the key
        d[key]["Parts"].append(a)
    wr.writerows(d.values()) # DictWriter will handle writing the rows

      

Output:

Parts,Value,Package,Amount
"['C1', 'C7', 'C10', 'C14', 'C22', 'C25', 'C27']",100n,C0402,7
"['C2', 'C6', 'C8', 'C9', 'C11', 'C12', 'C18', 'C19', 'C20', 'C31', 'C32']",100n,C0603,11
"['C3', 'C4', 'C13', 'C15', 'C28', 'C36']",1u,C0402,6
['C5'],10n-1%,C0402,1
"['C16', 'C33', 'C35', 'C40']",10u,C0402,4
"['C17', 'C24']",22u,C0603,2
"['C21', 'C56', 'C57', 'C58']",100u,A/3216-18R,4
"['C23', 'C26']",10n,C0402,2
['C29'],47uF,C0603,1
['C30'],0.5p,C0402,1
['C34'],120n,C0603,1
"['C34_X5R', 'C34_X5R1']",>2.2u,C0402,2
['C37'],6.8n,C0603,1
['C53'],1u,C0603,1
"['C54', 'C55']",12p,C0603,2
"['D1', 'D2']",PD3S160,POWERDI323,2
['IC1'],OPA333DBV,SOT23-5,1
['J1'],MOLEX_47346-0001,Molex-47346-0001,1
"['L3', 'L4']",BLM15HD182SN1,0402,2
['R1'],33M,R0603,1
"['R2', 'R4', 'R35']",10K,R0402,3
"['R3', 'R8', 'R11', 'R15', 'R19']",2k2,R0402,5
"['R5', 'R6']",10k,R0402,2
"['R7', 'R17', 'R21', 'R36']",10k,R0603,4
['R9'],220,R0603,1
"['R10', 'R12', 'R48', 'R49']",100,R0402,4
"['R13', 'R22']",4.7,R0402,2
"['R14', 'R16']",47,R0402,2
['R18'],15,R0603,1
['R20'],10,R0603,1
['R23'],47k,R0402,1
"['R24', 'R27']",1k,R0402,2
['R28'],20k,R0402,1
['R29'],0.2,R0603,1
['R42'],500,R0603,1
['SOT23-1'],LTC6240,SOT23-5,1
"['T1', 'T5']",PMV16UN,SOT-23,2
"['T2', 'T4', 'T6', 'T8']",FMMT617,SOT-23,4
['T3'],FMMT717,SOT-23,1
['U$1'],BPW34SMD,BPW_34_SMD,1
['U$2'],TPS73433,SOT23-5,1
"['U$4', 'U$5', 'U$6']",LED760/850_ROITHNER,DUOLED_5MM,3
['U$7'],TM4C123GH6ZRBBGA157,BGA_157_MICROSTAR,1
['U$8'],REF3225,SOT95P280X100-6N,1
['U$10'],TPS73633,SOT23-5,1
['U$12'],BNO055LGA-28,LGA-28,1
['U$14'],ECX-42_CRYSTALECS-160-9-42-CKM-TR,ECX-42_CRYSTAL,1
['U$16'],HEADER_1.27_7,HEADER_1.27_7,1
['U1'],ADS8860,SOP50P490X110-10N,1
['U6'],THS4281DBVT,SOT95P280X145-5N,1

      

+3


source


it

cn = Counter(tuple(islice(line.split(), 1, 3)) for line in f)

      

it should be



cn = Counter(tuple(islice(line.split(), 0, 3)) for line in f)

      

Edit: with an updated input file, the output is not the desired one:

C7,100nF,1
C11,100nF,1
C8,100nF,1

      

0


source


Here's a not-so-good approach:

  1 from collections import Counter
  2 from itertools import islice
  3 import sys
  4 
  5 t = open('Componentslist_Count.txt', 'wb')
  6 t.write("Part - Value - Package - Amount")
  7 
  8 def getPartDict(lines):
  9         tmp=[tuple(islice(line.split(), 0, 2)) for line in lines]
 10         for _t in tmp:
 11             if _t[1] in part_dict.keys():
 12                 part_dict[_t[1]].append(_t[0])
 13             else:
 14                 part_dict[_t[1]]=[_t[0]]
 15         return part_dict
 16 
 17 with open('test.txt') as f:
 18         part_dict={}
 19         #f.readline()
 20         f.readline()
 21         _f=f.readlines()
 22         cn = Counter(tuple(islice(line.split(), 1, 3)) for line in _f)
 23         part_dict=getPartDict(_f)
 24         for k, v in cn.items():
 25             t.write("\n")
 26             t.write("{l},{},{},{v}".format(l=part_dict[k[0]],*k, v=v))

      

Output:

  Part - Value - Package - Amount
  ['C3'],10uF_Tantalum,C0603,1
  ['X1'],53047-05,53047-05,1
  ['L1'],MURATA10UH,R1210,1
  ['R14'],19.6K,R0603,1
  ['TP5', 'TP6'],PTR1PAD1-13Y,P1-13Y,2
  ['R11', 'R13'],10K,R0603,2
  ['U$6'],TSV912DFN8_2X2,DFN8_2X2,1
  ['HDR_PRGRM_NIET_PLAATSEN'],HEADER_1.27_6P,HEADER_1.27_6,1
  ['R10'],10k,R0603,1
  ['U$9'],LTC3525_BOOST,SC70-6,1
  ['C14', 'C15'],2.2uF,C0603,2
  ['U$4'],MCP4921-E/MS,SOP65P490X110-8N,1
  ['C4'],22uF,C0603,1
  ['R1', 'R2'],22K,R0603,2
  ['U2'],ATMEGA32L-8MU,QFN50P700X700X100-45N,1
  ['C2', 'C6', 'C7', 'C8', 'C11', 'C12', 'C16', 'C17'],100nF,C0603,8
  ['U1'],REG71055DDCT,SOT95P180X110-6N,1
  ['C13'],0.22uF,C0603,1
  ['R4'],483,R0603,1
  ['R3', 'R5', 'R7', 'R12'],100,R0603,4
  ['C5'],1uF,C0603,1
  ['TP1', 'TP2', 'TP3', 'TP4'],PTR1TP20R,TP20R,4
  ['C1'],1nF,C0603,1
  ['U$1'],WML-C20,WML-C20,1

      

0


source







All Articles