pretty-print

How to format strings using printf() to get equal length in the output?

自作多情 提交于 2019-11-26 08:09:02
问题 I have two functions, one which produces messages like Starting initialization... and another which checks return codes and outputs \"Ok\" , \"Warning\" or \"Error\" . However, the output that is produced is of the different length: Starting initialization...Ok. Checking init scripts...Ok. How can I get something like this: Starting initialization... Ok. Checking init scripts... Ok. 回答1: You can specify width on string fields, e.g. printf("%-20s", "initialization..."); and then whatever's

Javascript: How to generate formatted easy-to-read JSON straight from an object? [duplicate]

末鹿安然 提交于 2019-11-26 07:52:38
问题 Possible Duplicate: How can I beautify JSON programmatically? I know how to generate JSON from an object using JSON.stringify, or in my case the handy jquery-json from google code (https://github.com/krinkle/jquery-json). Now this works fine, but the output is hard to read for humans. Is there an easy way / function / whatever to output a neatly formatted json file? This is what I mean: JSON.stringify({a:1,b:2,c:{d:1,e:[1,2]}}); gives.. \"{\"a\":1,\"b\":2,\"c\":{\"d\":1,\"e\":[1,2]}}\" I\'d

Convert JSON String to Pretty Print JSON output using Jackson

99封情书 提交于 2019-11-26 06:16:51
问题 This is the JSON string I have: {\"attributes\":[{\"nm\":\"ACCOUNT\",\"lv\":[{\"v\":{\"Id\":null,\"State\":null},\"vt\":\"java.util.Map\",\"cn\":1}],\"vt\":\"java.util.Map\",\"status\":\"SUCCESS\",\"lmd\":13585},{\"nm\":\"PROFILE\",\"lv\":[{\"v\":{\"Party\":null,\"Ads\":null},\"vt\":\"java.util.Map\",\"cn\":2}],\"vt\":\"java.util.Map\",\"status\":\"SUCCESS\",\"lmd\":41962}]} I need to convert the above JSON String into Pretty Print JSON Output (using Jackson), like below: { \"attributes\": [

The simplest way to comma-delimit a list?

Deadly 提交于 2019-11-26 05:00:10
问题 What is the clearest way to comma-delimit a list in Java? I know several ways of doing it, but I\'m wondering what the best way is (where \"best\" means clearest and/or shortest, not the most efficient. I have a list and I want to loop over it, printing each value. I want to print a comma between each item, but not after the last one (nor before the first one). List --> Item ( , Item ) * List --> ( Item , ) * Item Sample solution 1: boolean isFirst = true; for (Item i : list) { if (isFirst) {

How can I “pretty” format my JSON output in Ruby on Rails?

邮差的信 提交于 2019-11-26 03:46:24
问题 I would like my JSON output in Ruby on Rails to be \"pretty\" or nicely formatted. Right now, I call to_json and my JSON is all on one line. At times this can be difficult to see if there is a problem in the JSON output stream. Is there way to configure or a method to make my JSON \"pretty\" or nicely formatted in Rails? 回答1: Use the pretty_generate() function, built into later versions of JSON. For example: require 'json' my_object = { :array => [1, 2, 3, { :sample => "hash"} ], :foo => "bar

Formatting floats in Python without trailing zeros

青春壹個敷衍的年華 提交于 2019-11-26 03:16:17
问题 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\" 回答1: Me, I'd do ('%f' % x).rstrip('0').rstrip('.') -- guarantees fixed-point formatting rather than scientific notation, etc etc. Yeah, not as slick and elegant as %g , but, it works (and I don't know how to force %g to never use scientific notation;-). 回答2

How to turn off the Eclipse code formatter for certain sections of Java code?

﹥>﹥吖頭↗ 提交于 2019-11-26 03:15:07
问题 I\'ve got some Java code with SQL statements written as Java strings (please no OR/M flamewars, the embedded SQL is what it is - not my decision). I\'ve broken the SQL statements semantically into several concatenated strings over several lines of code for ease of maintenance. So instead of something like: String query = \"SELECT FOO, BAR, BAZ FROM ABC WHERE BAR > 4\"; I have something like: String query = \"SELECT FOO, BAR, BAZ\" + \" FROM ABC \" + \" WHERE BAR > 4 \"; This style makes the

Printing lists with commas C++

时光毁灭记忆、已成空白 提交于 2019-11-26 03:13:56
问题 I know how to do this in other languages, but not C++, which I am forced to use here. I have a Set of Strings that I\'m printing to out in a list, and they need a comma between each one, but not a trailing comma. In java for instance, I would use a stringbuilder and just delete the comma off the end after I\'ve built my string. How do I do it in C++? auto iter = keywords.begin(); for (iter; iter != keywords.end( ); iter++ ) { out << *iter << \", \"; } out << endl; I initially tried inserting

Pretty printing XML with javascript

主宰稳场 提交于 2019-11-26 02:41:43
问题 I have a string that represents a non indented XML that I would like to pretty-print. For example: <root><node/></root> should become: <root> <node/> </root> Syntax highlighting is not a requirement. To tackle the problem I first transform the XML to add carriage returns and white spaces and then use a pre tag to output the XML. To add new lines and white spaces I wrote the following function: function formatXml(xml) { var formatted = \'\'; var reg = /(>)(<)(\\/*)/g; xml = xml.replace(reg, \'

How can I pretty-print JSON in a shell script?

一曲冷凌霜 提交于 2019-11-26 01:34:22
问题 Is there a (Unix) shell script to format JSON in human-readable form? Basically, I want it to transform the following: { \"foo\": \"lorem\", \"bar\": \"ipsum\" } ... into something like this: { \"foo\": \"lorem\", \"bar\": \"ipsum\" } 回答1: With Python 2.6+ you can just do: echo '{"foo": "lorem", "bar": "ipsum"}' | python -m json.tool or, if the JSON is in a file, you can do: python -m json.tool my_json.json if the JSON is from an internet source such as an API, you can use curl http://my_url/