How Postgresql COPY TO STDIN With CSV do on conflic do update?

In this SO post, there are two answers that -combined together- provide a nice solution for successfully using ON CONFLICT. The example below, uses ON CONFLICT DO NOTHING;: BEGIN; CREATE TEMP TABLE tmp_table (LIKE main_table INCLUDING DEFAULTS) ON COMMIT DROP; COPY tmp_table FROM ‘full/file/name/here’; INSERT INTO main_table SELECT * FROM tmp_table ON CONFLICT DO NOTHING; … Read more

Python: Writing Nested Dictionary to CSV

This looks like a job for DictWriter: import csv import itertools import sys fields = [ ‘org’, ‘2015’, ‘2014’, ‘2013’ ] dw = { ‘orgname1’: { ‘2015’ : 2, ‘2014’ : 1, ‘2013’ : 1 }, ‘orgname2’: { ‘2015’ : 1, ‘2014’ : 2, ‘2013’ : 3 }, ‘orgname3’: { ‘2015’ : 1, ‘2014’ : … Read more

read csv/tsv with no header line in D3

Use d3.text to load the data, and then d3.csvParseRows to parse it. For example: d3.text(“data/testnh.csv”, function(text) { console.log(d3.csvParseRows(text)); }); You’ll probably also want to convert your columns to numbers, because they’ll be strings by default. Assume they are all numbers, you could say: d3.text(“data/testnh.csv”, function(text) { var data = d3.csvParseRows(text).map(function(row) { return row.map(function(value) { return … Read more

Delete blank rows from CSV?

Use the csv module: import csv … with open(in_fnam, newline=””) as in_file: with open(out_fnam, ‘w’, newline=””) as out_file: writer = csv.writer(out_file) for row in csv.reader(in_file): if row: writer.writerow(row) If you also need to remove rows where all of the fields are empty, change the if row: line to: if any(row): And if you also want … Read more