string-formatting

How to convert escaped characters in Python?

我的未来我决定 提交于 2019-11-28 00:13:04
I want to convert strings containing escaped characters to their normal form, the same way Python's lexical parser does: >>> escaped_str = 'One \\\'example\\\'' >>> print(escaped_str) One \'Example\' >>> normal_str = normalize_str(escaped_str) >>> print(normal_str) One 'Example' Of course the boring way will be to replace all known escaped characters one by one: http://docs.python.org/reference/lexical_analysis.html#string-literals How would you implement normalize_str() in the above code? >>> escaped_str = 'One \\\'example\\\'' >>> print escaped_str.encode('string_escape') One \\\'example\\\'

Does StringFormat feature of WPF Xaml work on Label.Content?

末鹿安然 提交于 2019-11-27 23:28:16
I have bind my Amount Label's Content Property to a decimal property via DataContext. I am trying to apply stringformat but see no effect. Does StringFormat feature work on Label controls ?? Please tell me on which controls does this feature work. BTW following is the code for the Label Control for whom i want to apply the currency formatting <Label Grid.Column="2" Content="{Binding Path=Amount, StringFormat={}{0:C}}" Height="23" HorizontalAlignment="Left" Margin="100,10,0,0" Name="tb" VerticalAlignment="Bottom" Width="120" /> StringFormat works on properties of type string (when the object

Python 3 bytes formatting

社会主义新天地 提交于 2019-11-27 23:24:46
In Python 3, one can format a string like: "{0}, {1}, {2}".format(1, 2, 3) But how to format bytes? b"{0}, {1}, {2}".format(1, 2, 3) raises AttributeError: 'bytes' object has no attribute 'format' . If there is no format method for bytes, how to do the formatting or "rewriting" of bytes? And as of 3.5 % formatting will work for bytes , too! https://mail.python.org/pipermail/python-dev/2014-March/133621.html Another way would be: "{0}, {1}, {2}".format(1, 2, 3).encode() Tested on IPython 1.1.0 & Python 3.2.3 Interestingly .format() doesn't appear to be supported for byte-sequences; as you have

Convert int to shortened, formatted string

生来就可爱ヽ(ⅴ<●) 提交于 2019-11-27 23:18:32
The user will enter a dollar value as an int , and I'd like to convert the result into a shortened, formatted string. So if the user enters 1700, the string would say "$1.7k". If the user enters 32600000, the string would say "$32.6m". Update Here's the code I have so far. It seems to be working for numbers ~10k. I would just add more if statements for bigger numbers. But is there a more efficient way to do this? NSNumberFormatter *nformat = [[NSNumberFormatter alloc] init]; [nformat setFormatterBehavior:NSNumberFormatterBehavior10_4]; [nformat setCurrencySymbol:@"$"]; [nformat setNumberStyle

StringFormat is ignored

拜拜、爱过 提交于 2019-11-27 23:04:50
问题 This is my binding (shortened, Command-Property is also bound) <MenuItem Header="Key" CommandParameter="{Binding StringFormat='Key: {0}', Path=PlacementTarget.Tag, RelativeSource={RelativeSource AncestorType=ContextMenu}}"/> The Tag-Property of ContectMenu's PlacementTarget is a String like "Short.Plural" What i expect to receive in the Command-Handler is: Key: Short.Plural But what i acutally receive is: Short.Plural 回答1: Label does not use StringFormat but ContentStringFormat. Use it this

Binding StringFormat

柔情痞子 提交于 2019-11-27 22:57:10
I have a collection of textblocks that I'm going to be showing and I'm needing the text of each textblock to be displayed differently. I'm currently saving the format string in the tag property and I'm needing to display the text in this format. How do I bind the StringFormat section? Something like the section below: <TextBlock Tag="{Binding MyFormatString}" Text="{Binding MyProperty, StringFormat='{}{0:MyTag}'}" /> Since BindingBase.StringFormat is not a dependency property, I do not think that you can bind it. If the formatting string varies, I'm afraid you will have to resort to something

Change default float print format

匆匆过客 提交于 2019-11-27 22:56:18
I've some lists and more complex structures containing floats. When printing them, I see the floats with a lot of decimal digits, but when printing, I don't need all of them. So I would like to define a custom format (e.g. 2 or 3 decimals) when floats are printed. I need to use floats and not Decimal. Also, I'm not allowed to truncate/round floats. Is there a way to change the default behavior? Donald Miner You are not allowed to monkeypatch C types, like Ignacio said. However, if you are terribly pressed in doing so and you know some C, you could go modify the Python interpreter source code

How do I format a double to a string and only show decimal digits when necessary?

扶醉桌前 提交于 2019-11-27 22:15:49
问题 I have code like: lblFranshizShowInvwNoskhehEdit.Text = string.Format("{0:n}", (double)(int.Parse(drDarman["FranshizDarsad"].ToString()) * Convert.ToInt64(RadNumerictxtPayInvwNoskhehEdit.Text)) / 100); But {0:n0} string format forces the label's text to not have decimal digits and {0:n} string format forces the label's text to have 2 decimal digits (default). In my scenario I just want decimal digits when necessary / without rounding them / how can I do that? 回答1: You can just do: string

How to format a number as percentage without the percentage sign?

隐身守侯 提交于 2019-11-27 22:03:08
问题 How do I in .NET format a number as percentage without showing the percentage sign? If I have the number 0.13 and use the format string {0:P0} the output is 13 % . However I would like to get 13 instead, without having to multiply the number by 100 and using the format string {0:N0} . (Background: In ASP.NET I have a GridView with a BoundField where I would like to display a number as percentage but without the percentage sign (%). How do I do that?) Thanks for the answers. At the time of

Javascript equivalent to python's .format()

六月ゝ 毕业季﹏ 提交于 2019-11-27 21:37:16
I would like a javascript function that mimics the python .format() function that works like .format(*args, **kwargs) A previous question gives a possible (but not complete) solution for '.format(*args) JavaScript equivalent to printf/string.format I would like to be able to do "hello {} and {}".format("you", "bob" ==> hello you and bob "hello {0} and {1}".format("you", "bob") ==> hello you and bob "hello {0} and {1} and {a}".format("you", "bob",a="mary") ==> hello you and bob and mary "hello {0} and {1} and {a} and {2}".format("you", "bob","jill",a="mary") ==> hello you and bob and mary and