Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Sounds like a <code>csv</code> problem to me.</p> <p>unfortunately, it is not clear from your question, if you want to modify the masterfile itself, an out-file, or both. this does the second (it takes a masterfile and an updatefile, both in csv format, and prints the merged thing unsorted to an out-file). If this is not what you want, or if you got data comma-seperated, but without fieldnames on top, change as you need, should be easy enough.</p> <pre><code>import csv with open("master.csv") as m, open("update.csv") as u, open("out.csv", "w") as o: master = { line['ID']: line for line in csv.DictReader(m) } update = { line['ID']: line for line in csv.DictReader(u) } master.update(update) fields = csv.DictReader(open("master.csv")).fieldnames out = csv.DictWriter(o, fields) out.writeheader() out.writerows(master.values()) </code></pre> <p>with master.csv as such:</p> <pre><code>ID,Name,Foo,Bar,Baz,Description 1000001,Name here:1,1001,1,description here 1000002,Name here:2,1002,2,description here 1000003,Name here:3,1003,3,description here 1000004,Name here:4,1004,4,description here 1000005,Name here:5,1005,5,description here 1000006,Name here:6,1006,6,description here 1000007,Name here:7,1007,7,description here 1000008,Name here:8,1008,8,description here 1000009,Name here:9,1009,9,description here </code></pre> <p>and update.csv as such:</p> <pre><code>ID,Name,Foo,Bar,Baz,Description 1000003,UPDATED Name here:3,1003,3, UPDATED description here 1000010,NEW ITEM Name here:9,1009,9,NEW ITEM description here </code></pre> <p>it outputs to out.csv:</p> <pre><code>ID,Name,Foo,Bar,Baz,Description 1000010,NEW ITEM Name here:9,1009,9,NEW ITEM description here , 1000008,Name here:8,1008,8,description here, 1000009,Name here:9,1009,9,description here, 1000006,Name here:6,1006,6,description here, 1000007,Name here:7,1007,7,description here, 1000004,Name here:4,1004,4,description here, 1000005,Name here:5,1005,5,description here, 1000002,Name here:2,1002,2,description here, 1000003,UPDATED Name here:3,1003,3, UPDATED description here, 1000001,Name here:1,1001,1,description here, </code></pre> <p>Note that the order is <em>not</em> preserved (not clear from question if neccessary). But it is fast and clean.</p>
 

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