string-formatting

Pandas DataFrame formatting

天涯浪子 提交于 2019-12-05 15:57:14
I have a pandas DataFrame with mixed values in it. I am working in Ipython notebook while developing it. When displaying the dataframe I would like it to display to facilitate easier reading. At the moment I am using python string formatting to display all floats to 4 decimals and to add thousand separators. pd.options.display.float_format = '{:,.4f}'.format Ideally I would like to E.g. Display Values over 10000 without decimals, fractions with 4 significant digits etc. Is there a way that I can use the python string formatting syntax to achieve this? I know i can do it for individual column,

String.Format in C# not returning modified int value

不羁岁月 提交于 2019-12-05 15:27:41
I am tring to return an int value with comma seperators within the value. 12345 would be returned as 12,345 The follwing code works: int myInt = 1234567; MessageBox.Show(string.Format("My number is {0}", myInt.ToString("#,#"))); 12,345 is displayed as expected. While the following code does no work, but from what I am reading, should work: int myInt = 1234567; MessageBox.Show(string.Format("My number is {0:#,#}", myInt.ToString())); 12345 is displayed. Can you help me understand why the second set of code is not working? Thanks You shouldn't ToString the int before the format. Try this:

What is the easiest way to pad a string with 0 to the left?

不羁的心 提交于 2019-12-05 14:35:12
问题 What is the easiest way to pad a string with 0 to the left so that "110" = "00000110" "11110000" = "11110000" I have tried to use the format! macro but it only pads to the right with space: format!("{:08}", string); 回答1: The fmt module documentation describes all the formatting options: Fill / Alignment The fill character is provided normally in conjunction with the width parameter. This indicates that if the value being formatted is smaller than width some extra characters will be printed

Caused by: java.lang.IllegalArgumentException: Bad class: class java.lang.String

筅森魡賤 提交于 2019-12-05 12:56:51
I'm trying to format dates which I retrieve from my database table, I wanted to format from "2015 02 11 12 00" to "Feb 2" but it gives an error when I format it. Is there any other ways formatting those dates? Following the codes: protected String doInBackground(String... args) { // Building Parameters List<NameValuePair> params = new ArrayList<NameValuePair>(); // getting JSON string from URL JSONObject json = jParser.makeHttpRequest(url_all_events, "GET", params); // Check your log cat for JSON reponse Log.d("All events: ", json.toString()); try { // Checking for SUCCESS TAG int success =

python string replacement with % character/**kwargs weirdness

落爺英雄遲暮 提交于 2019-12-05 11:34:47
Following code: def __init__(self, url, **kwargs): for key in kwargs.keys(): url = url.replace('%%s%' % key, str(kwargs[key])) Throws the following exception: File "/home/wells/py-mlb/lib/fetcher.py", line 25, in __init__ url = url.replace('%%s%' % key, str(kwargs[key])) ValueError: incomplete format The string has a format like: http://www.blah.com?id=%PLAYER_ID% What am I doing wrong? You probably want the format string %%%s%% instead of %%s% . Two consecutive % signs are interpreted as a literal % , so in your version, you have a literal % , a literal s , and then a lone % , which is

Converting MM:SS.ms to seconds using MS excel

夙愿已清 提交于 2019-12-05 11:33:30
问题 I am looking for a neat way of converting a cell from Minutes:Seconds.Milliseconds to Seconds.Milliseconds i.e. 11.111 = 11.111 1:11.111 = 71.111 I have something in place at the moment but its a bit hacky and I am sure there must be some nice excel feature to do this for me :P Thanks! 回答1: Do this: Place values 0:0:11.111 and 0:1:11.111 in cells B3 and B4 respectively. Now format it to account for the milliseconds... Select cells B3 and B4 , right click and choose Format Cells. In Custom,

PyCharm and f-strings

狂风中的少年 提交于 2019-12-05 10:38:19
I am using the latest stable PyCharm 2016.1.4 and Python 3.6a1. Whenever I use the "f-strings" ( PEP-498 ) PyCharm is complaining about f being an unresolved reference : Is the literal string interpolation not supported by PyCharm yet? Or, should I have enabled or configured it separately? The Literal String Interpolation is now supported in PyCharm 2016.3 , the relevant feature request: PY-18972 implement support for PEP 498 (f-strings) Note that the stable 3.6 is scheduled to be released in December . 来源: https://stackoverflow.com/questions/38215059/pycharm-and-f-strings

HTML formatting for TextView

痴心易碎 提交于 2019-12-05 10:37:48
I am a bit confused about the 'rules' of when a TextView element displays text in formatted form or not. A string like "There are <i>different ways</i> of coding.\n"; displays without any formatting (including the HTML codes) when I code tvMyTextView.setText("There are <i>different ways</i> of coding.\n"); but when I define the same string in strings.xml and then load tvMyTextView.setText(R.strings.TestString); it displays emphasized. Even more confused I feel when trying to embed URLs in TextView's like here: "Click <a href="http://www.poon-world.com">here</a> to switch on the red light.\n";

how to format 1700 to 1'700 and 1000000 to 1'000'000 in c#?

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-05 10:33:25
I like to format all numbers like in math. is there a predefined function or is that just possible with substring and replace? edit: my culture is de-ch Best regards Try this int input = Convert.ToInt32("1700"); string result = String.Format("{0:##,##}", input); Or this Console.WriteLine(1700.ToString("##,##", new NumberFormatInfo() { NumberGroupSeparator = "'" })); var numformat = new NumberFormatInfo { NumberGroupSeparator = "'", NumberGroupSizes = new int[] { 3 }, NumberDecimalSeparator = "." }; Console.WriteLine(1000000.ToString("N",numformat)); Try this: Console.WriteLine(1000000.ToString

Formatting a nan float in python

风流意气都作罢 提交于 2019-12-05 10:28:19
I'm trying to use string.format on a 'nan' float. Here's the description of the 'g' option from the python documentation . General format. This prints the number as a fixed-point number, unless the number is too large, in which case it switches to 'e' exponent notation. Infinity and NaN values are formatted as inf, -inf and nan, respectively. And here's what i get trying it in the interpreter (Python 2.6): >>> print "{0:g}".format(float('nan')) -1.#IND As I understand the documentation, the output should be "nan". Is this a bug or am I doing it wrong? repr(float) was fixed in Python 2.6 and