How do I get Python’s ElementTree to pretty print to an XML file?

Whatever your XML string is, you can write it to the file of your choice by opening a file for writing and writing the string to the file. from xml.dom import minidom xmlstr = minidom.parseString(ET.tostring(root)).toprettyxml(indent=” “) with open(“New_Database.xml”, “w”) as f: f.write(xmlstr) There is one possible complication, especially in Python 2, which is both less … Read more

Pretty-print a NumPy array without scientific notation and with given precision

Use numpy.set_printoptions to set the precision of the output: import numpy as np x = np.random.random(10) print(x) # [ 0.07837821 0.48002108 0.41274116 0.82993414 0.77610352 0.1023732 # 0.51303098 0.4617183 0.33487207 0.71162095] np.set_printoptions(precision=3) print(x) # [ 0.078 0.48 0.413 0.83 0.776 0.102 0.513 0.462 0.335 0.712] And suppress suppresses the use of scientific notation for small numbers: … Read more

How can I beautify JavaScript code using Command Line?

First, pick your favorite Javascript based Pretty Print/Beautifier. I prefer the one at http://jsbeautifier.org/, because it’s what I found first. Downloads its file https://github.com/beautify-web/js-beautify/blob/master/js/lib/beautify.js Second, download and install The Mozilla group’s Java based Javascript engine, Rhino. “Install” is a little bit misleading; Download the zip file, extract everything, place js.jar in your Java classpath (or … Read more

The simplest way to comma-delimit a list?

Java 8 and later Using StringJoiner class, and forEach method : StringJoiner joiner = new StringJoiner(“,”); list.forEach(item -> joiner.add(item.toString()); return joiner.toString(); Using Stream, and Collectors: return list.stream(). map(Object::toString). collect(Collectors.joining(“,”)).toString(); Java 7 and earlier See also #285523 String delim = “”; for (Item i : list) { sb.append(delim).append(i); delim = “,”; }

Pretty printing XML with javascript

From the text of the question I get the impression that a string result is expected, as opposed to an HTML-formatted result. If this is so, the simplest way to achieve this is to process the XML document with the identity transformation and with an <xsl:output indent=”yes”/> instruction: <xsl:stylesheet version=”1.0″ xmlns:xsl=”http://www.w3.org/1999/XSL/Transform”> <xsl:output omit-xml-declaration=”yes” indent=”yes”/> <xsl:template … Read more

How can I beautify JSON programmatically? [duplicate]

Programmatic formatting solution: The JSON.stringify method supported by many modern browsers (including IE8) can output a beautified JSON string: JSON.stringify(jsObj, null, “\t”); // stringify with tabs inserted at each level JSON.stringify(jsObj, null, 4); // stringify with 4 spaces at each level Demo: http://jsfiddle.net/AndyE/HZPVL/ This method is also included with json2.js, for supporting older browsers. Manual … Read more