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 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

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

Pretty-Print JSON in Java

Google’s GSON can do this in a nice way: Gson gson = new GsonBuilder().setPrettyPrinting().create(); JsonParser jp = new JsonParser(); JsonElement je = jp.parse(uglyJsonString); String prettyJsonString = gson.toJson(je); or since it is now recommended to use the static parse method from JsonParser you can also use this instead: Gson gson = new GsonBuilder().setPrettyPrinting().create(); JsonElement je = … Read more

How to pretty-print a numpy.array without scientific notation and with given precision?

You can use 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