Note that there are some explanatory texts on larger screens.

plurals
  1. POCompare two CSV files against a master CSV file and merge the results into a new file
    primarykey
    data
    text
    <p>I have 3 CSV files. I have one master CSV document that holds the bulk of the information I'll need down the road. But is missing data from two other CSV documents. The goal is to compare the master document to each of the other CSVs, pull the specified data from the 2 CSVs and add that info to the rows in the master CSV. Finally writing them to a new file.</p> <p>Here is what I've gotten so far, not that it works.</p> <p><strong>inv</strong> = is the master document, it has 99% of the information I need</p> <p><strong>vb</strong> = has in common 'PART CODE' with inv's '\xef\xbb\xbfPART_CODE', I need it's 'ON-HAND' added to inv's row </p> <p><strong>main</strong> = has in common 'PART CODE' with inv's '\xef\xbb\xbfPART_CODE', I need it's 'ON-HAND' added to inv's row </p> <pre><code>import csv inv = csv.DictReader(open('ireport.txt', 'rU'), dialect='excel-tab', delimiter="\t") vb = csv.DictReader(open('vb.txt', 'rU'), dialect='excel-tab', delimiter="\t") main = csv.DictReader(open('main.txt', 'rU'), dialect='excel-tab', delimiter="\t") vblist = [] mainlist = [] def vbfunc(dictreader1, dictreader2): dictreader2 = list(dictreader2) for dictline1 in dictreader1: for dictline2 in dictreader2: if dictline1['PART CODE'] == dictline2['\xef\xbb\xbfPART_CODE']: dictline2['VB'] = dictline1['ON-HAND'] else: dictline2['VB'] = "0" vblist.append(dictline2) def mainfunc(dictreader1, dictreader2): dictreader2 = list(dictreader2) for dictline1 in dictreader1: for dictline2 in dictreader2: if dictline1['PART CODE'] == dictline2['\xef\xbb\xbfPART_CODE']: dictline2['MAIN'] = dictline1['ON-HAND'] else: dictline2['MAIN'] = "0" mainlist.append(dictline2) vbfunc(vb,inv) #mainfuc(main,inv) #I'll get to this when the other function works for i in vblist: print i['VB'] #complains of KeyError: VB </code></pre> <p>1st problem:</p> <pre><code>if dictline1['PART CODE'] == dictline2['\xef\xbb\xbfPART_CODE']: dictline2['VB'] = dictline1['ON-HAND'] else: dictline2['VB'] = "0" </code></pre> <p>When it's added to the list, it treats every result as "0", if I omit the else: dictline2['VB'] = "0" part, it reports as expected. But if not left in, it will later complain of KeyError: 'VB' when I try to print i['VB']</p> <p>2nd problem:</p> <pre><code>for i in vblist: print i['VB'] #complains of KeyError: VB </code></pre> <p>As stated, complains of a KeyError. If I simply do print i, it displays that the key/value for i is there, it'll report 'VB': '0', etc.</p> <p>I'm still very green to programming, been at this hours a night for a week and I've become discouraged. But not enough to give up!</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload