Python JSON dump / append to .txt with each variable on new line

Your question is a little unclear. If you’re generating hostDict in a loop: with open(‘data.txt’, ‘a’) as outfile: for hostDict in ….: json.dump(hostDict, outfile) outfile.write(‘\n’) If you mean you want each variable within hostDict to be on a new line: with open(‘data.txt’, ‘a’) as outfile: json.dump(hostDict, outfile, indent=2) When the indent keyword argument is set … Read more

jQuery: append() object, remove() it with delay()

Using setTimeout() directly (which .delay() uses internally) is simpler here, since .remove() isn’t a queued function, overall it should look like this: $(‘body’).append(“<div class=”message success”>Upload successful!</div>”); setTimeout(function() { $(‘.message’).remove(); }, 2000); You can give it a try here. .delay() is for the animation (or whatever named) queue, to use it you’d have to do something … Read more

Getting the height of an element before added to the DOM

Elements don’t have a height, in any real sense, until they’ve been added to the DOM, as their styles cannot be evaluated until then. You can get around this easily enough using visibility: hidden so that the element can be added to the DOM (and its height determined) without causing visible flickering. function test(a) { … Read more

How to save a hash into a CSV

If you want column headers and you have multiple hashes: require ‘csv’ hashes = [{‘a’ => ‘aaaa’, ‘b’ => ‘bbbb’}] column_names = hashes.first.keys s=CSV.generate do |csv| csv << column_names hashes.each do |x| csv << x.values end end File.write(‘the_file.csv’, s) (tested on Ruby 1.9.3-p429)