How do I pretty-print HTML with Nokogiri?

The answer by @mislav is somewhat wrong. Nokogiri does support pretty-printing if you: Parse the document as XML Instruct Nokogiri to ignore whitespace-only nodes (“blanks”) during parsing Use to_xhtml or to_xml to specify pretty-printing parameters In action: html=”<section> <h1>Main Section 1</h1><p>Intro</p> <section> <h2>Subhead 1.1</h2><p>Meat</p><p>MOAR MEAT</p> </section><section> <h2>Subhead 1.2</h2><p>Meat</p> </section></section>” require ‘nokogiri’ doc = Nokogiri::XML(html,&:noblanks) puts … Read more

Pretty print a tree

If you want it to be very pretty, you could steal about 25 lines of code from this blog entry to draw it with WPF. But I’ll code up an ascii solution shortly too, probably. EDIT Ok, wow, that was hard. I’m not certain it’s entirely correct, and I can’t help but think there’s probably … Read more

Printing the same character several times without a loop

The original answer is from 2014, so there must have been some updates to the Dart language: a simple string multiplied by an int works. main() { String title=”Dart: Strings can be “multiplied””; String line=”-” * title.length print(line); print(title); print(line); } And this will be printed as: ——————————— Dart: Strings can be “multiplied” ——————————— See … Read more

Pretty print XML in java 8

In reply to Espinosa’s comment, here is a solution when “the original xml is not already (partially) indented or contain new lines“. Background Excerpt from the article (see References below) inspiring this solution: Based on the DOM specification, whitespaces outside the tags are perfectly valid and they are properly preserved. To remove them, we can … Read more

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

Python pretty XML printer with lxml

For me, this issue was not solved until I noticed this little tidbit here: http://lxml.de/FAQ.html#why-doesn-t-the-pretty-print-option-reformat-my-xml-output Short version: Read in the file with this command: >>> parser = etree.XMLParser(remove_blank_text=True) >>> tree = etree.parse(filename, parser) That will “reset” the already existing indentation, allowing the output to generate it’s own indentation correctly. Then pretty_print as normal: >>> tree.write(<output_file_name>, … Read more

Custom indent width for BeautifulSoup .prettify()

I actually dealt with this myself, in the hackiest way possible: by post-processing the result. r = re.compile(r’^(\s*)’, re.MULTILINE) def prettify_2space(s, encoding=None, formatter=”minimal”): return r.sub(r’\1\1′, s.prettify(encoding, formatter)) Actually, I monkeypatched prettify_2space in place of prettify in the class. That’s not essential to the solution, but let’s do it anyway, and make the indent width a … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)