pretty-print

Empty lines while using minidom.toprettyxml

Empty lines while using minidom.toprettyxml Question: I’ve been using a minidom.toprettyxml for prettify my xml file. When I’m creating XML file and using this method, all works grate, but if I use it after I’ve modified the xml file (for examp I’ve added an additional nodes) and then I’m writing it back to XML, I’m …

Total answers: 7

How to prettyprint a JSON file?

How to prettyprint a JSON file? Question: How do I pretty-print a JSON file in Python? Asked By: Colleen || Source Answers: Use the indent= parameter of json.dump() or json.dumps() to specify how many spaces to indent by: >>> import json >>> your_json = ‘["foo", {"bar": ["baz", null, 1.0, 2]}]’ >>> parsed = json.loads(your_json) >>> …

Total answers: 15

Memory dump formatted like xxd from gdb

Memory dump formatted like xxd from gdb Question: I’m trying to inspect a buffer which contains a binary formatted message, but also contains string data. As an example, I’m using this C code: int main (void) { char buf[100] = “x01x02x03x04String DataxAAxBBxCC”; return 0; } I’d like to get a hex dump of what’s in …

Total answers: 5

Pretty-Print JSON Data to a File using Python

Pretty-Print JSON Data to a File using Python Question: A project for class involves parsing Twitter JSON data. I’m getting the data and setting it to the file without much trouble, but it’s all in one line. This is fine for the data manipulation I’m trying to do, but the file is ridiculously hard to …

Total answers: 8

Pretty print in lxml is failing when I add tags to a parsed tree

Pretty print in lxml is failing when I add tags to a parsed tree Question: I have an xml file that I’m using etree from lxml to work with, but when I add tags to it, pretty printing doesn’t seem to work. >>> from lxml import etree >>> root = etree.parse(‘file.xml’).getroot() >>> print etree.tostring(root, pretty_print …

Total answers: 2

How to Pretty Print HTML to a file, with indentation

How to Pretty Print HTML to a file, with indentation Question: I am using lxml.html to generate some HTML. I want to pretty print (with indentation) my final result into an html file. How do I do that? This is what I have tried and got till now import lxml.html as lh from lxml.html import …

Total answers: 11

Python pretty XML printer with lxml

Python pretty XML printer with lxml Question: After reading from an existing file with ‘ugly’ XML and doing some modifications, pretty printing doesn’t work. I’ve tried etree.write(FILE_NAME, pretty_print=True). I have the following XML: <testsuites tests=”14″ failures=”0″ disabled=”0″ errors=”0″ time=”0.306″ name=”AllTests”> <testsuite name=”AIR” tests=”14″ failures=”0″ disabled=”0″ errors=”0″ time=”0.306″> …. And I use it like this: tree …

Total answers: 6

How to Print "Pretty" String Output in Python

How to Print "Pretty" String Output in Python Question: I have a list of dicts with the fields classid, dept, coursenum, area, and title from a sql query. I would like to output the values in a human readable format. I was thinking a Column header at the top of each and then in each …

Total answers: 5

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

How to pretty-print a numpy.array without scientific notation and with given precision? Question: I’m curious, whether there is any way to print formatted numpy.arrays, e.g., in a way similar to this: x = 1.23456 print ‘%.3f’ % x If I want to print the numpy.array of floats, it prints several decimals, often in ‘scientific’ format, …

Total answers: 14

Formatting floats without trailing zeros

Formatting floats without trailing zeros Question: How can I format a float so that it doesn’t contain trailing zeros? In other words, I want the resulting string to be as short as possible. For example: 3 -> “3” 3. -> “3” 3.0 -> “3” 3.1 -> “3.1” 3.14 -> “3.14” 3.140 -> “3.14” Asked By: …

Total answers: 21