pretty-print

Use jq to Format Certain Fields as Compact?

元气小坏坏 提交于 2019-12-02 05:04:38
Is jq the best choice for pretty-printing arbitrary JSON? cat my.json | jq . pretty-prints the given JSON, but expands every field on a separate line. But what if some of the fields are repetitive, such as a list of points? How can fields that match a pattern be formatted on a single line with --compact-output ? For example, format "coords" and "list" fields below on a single line: [ { "field1": { "a": "", "b": "" "list": [{ "name": "x", "score": 1, "rect": { "x": 156, "y": 245, "w": 35, "h": 45 }, ... ] }, "field2": 2, "coords": [{ "x": 100, "y": 400 },{ "x": 100, "y": 0 }] }, .... ] The

XML pretty print fails in Python lxml

 ̄綄美尐妖づ 提交于 2019-12-02 02:28:55
I am trying to read, modify, and write an XML file with lxml 4.1.1 in Python 2.7.6. My code: import lxml.etree as et fn_xml_in = 'in.xml' parser = et.XMLParser(remove_blank_text=True) xml_doc = et.parse(fn_xml_in, parser) xml_doc.getroot().find('b').append(et.Element('c')) xml_doc.write('out.xml', method='html', pretty_print=True) The input file in.xml looks like this: <a> <b/> </a> And the produced output file out.xml : <a> <b><c></c></b> </a> Or when I set remove_blank_text=True : <a><b><c></c></b></a> I would have expected lxml to insert line breaks and indentation within the b element: <a>

Compact but pretty JSON output in python?

强颜欢笑 提交于 2019-12-01 18:13:10
JSON is written either with indent=None (default) as a single line (unreadable to a human eye) or with ident=N with a newline after each comma. What I would like to see is a more compact but still pretty output, similar to what Common Lisp pretty-printing does. E.g., instead of { "cleanup":{ "cpu":6936.780000000001, "wall":7822.319401979446 }, "finished":"2017-08-14 18:36:23", "init":{ "cpu":1365.73, "wall":1380.7802910804749 }, "job":"timings", "run":{ "cpu":953.6700000000001, "wall":8350.496850013733 }, "started":"2017-08-14 13:28:06" } I would like to see { "cleanup":{"cpu":6936

Format list output in Haskell?

送分小仙女□ 提交于 2019-12-01 16:47:20
I am having trouble trying to format the output a of a list of my own type in Haskell. I would like something like this: Make | Model | Years(this is a list) <- this would be the headers if you like ------------------- Item1 | Item1 | Item1s,Item1s Item2 | Item2 | Item2s,Items2,Items2 ^ This would be the data loaded from my String String [Int] type. How would I do this in Haskell? Something like this? import Data.List (intercalate) data Foo = Foo String String [Int] fooToLine :: Foo -> String fooToLine (Foo a b cs) = a ++ " | " ++ b ++ " | " ++ intercalate ", " (map show cs) Now, you can do >>

round number to the first 3 digits (start with digit != 0)

眉间皱痕 提交于 2019-12-01 16:09:19
is there a predefined format-function that rounds a number to the first 3 digits? (The start should be a numbers != 0) -0.02528498 to -0.0253 1.857403 to 1.86 2060943 to 2060000 0.00006513832 to 0.0000651 You can use the function signif : signif(-0.02528498, 3) # [1] -0.0253 signif(1.857403, 3) # [1] 1.86 signif(2060943, 3) # [1] 2060000 signif(0.00006513832, 3) # [1] 0.0000651 来源: https://stackoverflow.com/questions/29674349/round-number-to-the-first-3-digits-start-with-digit-0

Format list output in Haskell?

南笙酒味 提交于 2019-12-01 15:47:19
问题 I am having trouble trying to format the output a of a list of my own type in Haskell. I would like something like this: Make | Model | Years(this is a list) <- this would be the headers if you like ------------------- Item1 | Item1 | Item1s,Item1s Item2 | Item2 | Item2s,Items2,Items2 ^ This would be the data loaded from my String String [Int] type. How would I do this in Haskell? 回答1: Something like this? import Data.List (intercalate) data Foo = Foo String String [Int] fooToLine :: Foo ->

Changing the default indentation of etree.tostring in lxml

与世无争的帅哥 提交于 2019-12-01 15:27:56
I have an XML document which I'm pretty-printing using lxml.etree.tostring print etree.tostring(doc, pretty_print=True) The default level of indentation is 2 spaces, and I'd like to change this to 4 spaces. There isn't any argument for this in the tostring function; is there a way to do this easily with lxml? As said in this thread , there is no real way to change the indent of the lxml.etree.tostring pretty-print. But, you can: add a XSLT transform to change the indent add whitespace to the tree, with something like in the cElementTree library code: def indent(elem, level=0): i = "\n" + level

round number to the first 3 digits (start with digit != 0)

丶灬走出姿态 提交于 2019-12-01 15:00:48
问题 is there a predefined format-function that rounds a number to the first 3 digits? (The start should be a numbers != 0) -0.02528498 to -0.0253 1.857403 to 1.86 2060943 to 2060000 0.00006513832 to 0.0000651 回答1: You can use the function signif : signif(-0.02528498, 3) # [1] -0.0253 signif(1.857403, 3) # [1] 1.86 signif(2060943, 3) # [1] 2060000 signif(0.00006513832, 3) # [1] 0.0000651 来源: https://stackoverflow.com/questions/29674349/round-number-to-the-first-3-digits-start-with-digit-0

Changing the default indentation of etree.tostring in lxml

核能气质少年 提交于 2019-12-01 14:18:44
问题 I have an XML document which I'm pretty-printing using lxml.etree.tostring print etree.tostring(doc, pretty_print=True) The default level of indentation is 2 spaces, and I'd like to change this to 4 spaces. There isn't any argument for this in the tostring function; is there a way to do this easily with lxml? 回答1: As said in this thread, there is no real way to change the indent of the lxml.etree.tostring pretty-print. But, you can: add a XSLT transform to change the indent add whitespace to

Sort xml attributes for pretty print using javax.xml.transform.Transformer

天大地大妈咪最大 提交于 2019-12-01 10:31:08
Is there a way I could tell the xml transformer to sort alphabetically all the attributes for the tags of a given XML? So lets say... <MyTag paramter1="lol" andTheOtherThing="potato"/> Would turn into <MyTag andTheOtherThing="potato" paramter1="lol"/> I saw how to format it from the examples I found here and here , but sorting the tag attributes would be the last issue I have. I was hoping there was something like: transformer.setOutputProperty(OutputKeys.INDENT, "yes"); transformer.setOutputProperty(OutputKeys.SORTATT, "yes"); // <-- no such thing Which seems to be what they say: http://docs