How to Print “Pretty” String Output in Python

Standard Python string formatting may suffice. # assume that your data rows are tuples template = “{0:8}|{1:10}|{2:15}|{3:7}|{4:10}” # column widths: 8, 10, 15, 7, 10 print template.format(“CLASSID”, “DEPT”, “COURSE NUMBER”, “AREA”, “TITLE”) # header for rec in your_data_source: print template.format(*rec) Or # assume that your data rows are dicts template = “{CLASSID:8}|{DEPT:10}|{C_NUM:15}|{AREA:7}|{TITLE:10}” # same, but … Read more

How to Pretty Print HTML to a file, with indentation

I ended up using BeautifulSoup directly. That is something lxml.html.soupparser uses for parsing HTML. BeautifulSoup has a prettify method that does exactly what it says it does. It prettifies the HTML with proper indents and everything. BeautifulSoup will NOT fix the HTML, so broken code, remains broken. But in this case, since the code is … Read more

Disabling sorting mechanism in pprint output

Python 3.8 or newer: Use sort_dicts=False: pprint.pprint(data, sort_dicts=False) Python 3.7 or older: You can monkey patch the pprint module. import pprint pprint.pprint({“def”:2,”ghi”:3,”abc”:1,}) pprint._sorted = lambda x:x # Or, for Python 3.7: # pprint.sorted = lambda x, key=None: x pprint.pprint({“def”:2,”ghi”:3, “abc”:1}) Since the 2nd output is essentiallly randomly sorted, your output may be different from mine: … Read more

How can I get Express to output nicely formatted HTML?

In your main app.js or what is in it’s place: Express 4.x if (app.get(‘env’) === ‘development’) { app.locals.pretty = true; } Express 3.x app.configure(‘development’, function(){ app.use(express.errorHandler()); app.locals.pretty = true; }); Express 2.x app.configure(‘development’, function(){ app.use(express.errorHandler()); app.set(‘view options’, { pretty: true }); }); I put the pretty print in development because you’ll want more efficiency with … Read more

How do I print out a tree structure?

The trick is to pass a string as the indent and to treat the last child specially: class Node { public void PrintPretty(string indent, bool last) { Console.Write(indent); if (last) { Console.Write(“\\-“); indent += ” “; } else { Console.Write(“|-“); indent += “| “; } Console.WriteLine(Name); for (int i = 0; i < Children.Count; i++) … Read more

How can I pretty-print XML source using VB6 and MSXML?

After months of research I’ve come up with this. Public Function PrettyPrintXML(XML As String) As String Dim Reader As New SAXXMLReader60 Dim Writer As New MXXMLWriter60 Writer.indent = True Writer.standalone = False Writer.omitXMLDeclaration = False Writer.encoding = “utf-8” Set Reader.contentHandler = Writer Set Reader.dtdHandler = Writer Set Reader.errorHandler = Writer Call Reader.putProperty(“http://xml.org/sax/properties/declaration-handler”, _ Writer) Call … Read more

Pretty-printing output from javax.xml.transform.Transformer with only standard java api (Indentation and Doctype positioning)

The missing part is the amount to indent. You can set the indentation and indent amount as follow: transformer.setOutputProperty(OutputKeys.INDENT, “yes”); transformer.setOutputProperty(“{http://xml.apache.org/xslt}indent-amount”, “2”); transformer.transform(xmlInput, xmlOutput);