string-formatting

String formatting with “{0:d}”.format gives Unknown format code 'd' for object of type 'float'

爱⌒轻易说出口 提交于 2019-12-05 10:26:00
问题 If I understood the docs correctly, in python 2.6.5 string formatting "{0:d}" would do the same as "%d" with the String.format() way of formatting strings " I have {0:d} dollars on me ".format(100.113) Should print "I have 100 dollars on me " However I get the error : ValueError: Unknown format code 'd' for object of type 'float' The other format operations do work.for eg. >>> "{0:e}".format(112121.2111) '1.121212e+05' 回答1: That error is signifying that you are passing a float to the format

How to do string formatting with unicode emdash?

一个人想着一个人 提交于 2019-12-05 10:02:09
I am trying do string formatting with a unicode variable. For example: >>> x = u"Some text—with an emdash." >>> x u'Some text\u2014with an emdash.' >>> print(x) Some text—with an emdash. >>> s = "{}".format(x) Traceback (most recent call last): File "<stdin>", line 1, in <module> UnicodeEncodeError: 'ascii' codec can't encode character u'\u2014' in position 9: ordinal not in range(128) >>> t = "%s" %x >>> t u'Some text\u2014with an emdash.' >>> print(t) Some text—with an emdash. You can see that I have a unicode string and that it prints just fine. The trouble is when I use Python's new (and

Custom string formatter in C#

﹥>﹥吖頭↗ 提交于 2019-12-05 08:49:57
String formatting in C#; Can I use it? Yes. Can I implement custom formatting? No. I need to write something where I can pass a set of custom formatting options to string.Format , which will have some effect on the particular item. at the moment I have something like this: string.Format("{0}", item); but I want to be able to do things with that item: string.Format("{0:lcase}", item); // lowercases the item string.Format("{0:ucase}", item); // uppercases the item string.Format("{0:nospace}", item); // removes spaces I know I can do things like .ToUpper() , .ToLower() etc. but I need to do it

StringFormat in XAML

狂风中的少年 提交于 2019-12-05 06:43:53
I am trying to format my string to have commas every 3 places, and a decimal if it is not a whole number. I have checked roughly 20 examples, and this is the closest I have come: <TextBlock x:Name="countTextBlock" Text="{Binding Count, StringFormat={0:n}}" /> But I get a The property 'StringFormat' was not found in type 'Binding'. error. Any ideas what is wrong here? Windows Phone 8.1 appears to differ from WPF, because all of the WPF resources say that this is how it is done. (The string is updated constantly, so I need the code to be in the XAML . I also need it to remain binded. Unless of

Using .NET string formatting, how do I format a string to display blank (empty string) for zero (0)?

大兔子大兔子 提交于 2019-12-05 06:18:41
I am using a DataBinder.Eval expression in an ASP.NET Datagrid, but I think this question applies to String formatting in .NET in general. The customer has requested that if the value of a string is 0, it should not be displayed. I have the following hack to accomplish this: <%# IIf(DataBinder.Eval(Container.DataItem, "MSDWhole").Trim = "0", "", DataBinder.Eval(Container.DataItem, "MSDWhole", "{0:N0}")) %> I would like to change the {0:N0} formatting expression so that I can eliminate the IIf statement, but can't find anything that works. You need to use the section separator , like this: <%#

How to format std::chrono durations?

我的未来我决定 提交于 2019-12-05 06:05:20
Is there a convenient way to format std::chrono::duration to a specified format? std::chrono::high_resolution_clock::time_point now, then; then = std::chrono::high_resolution_clock::now(); // ... now = std::chrono::high_resolution_clock::now(); auto duration = now - then; // base in microseconds: auto timeInMicroSec = std::chrono::duration_cast<std::chrono::microseconds>(duration); How can I format timeInMicroSec like ss::ms::us ? One can use something like: #include <iomanip> #include <sstream> //... auto c(timeInMicroSec.count()); std::ostringstream oss; oss << std::setfill('0') // set field

IndexError: tuple index out of range when parsing method arguments

北战南征 提交于 2019-12-05 05:48:45
I've already checked this question, but couldn't find an answer there. Here is a simple example that demonstrates my use case: def log(*args): message = str(args[0]) arguments = tuple(args[1:]) # message itself print(message) # arguments for str.format()0 print(arguments) # shows that arguments have correct indexes for index, value in enumerate(arguments): print("{}: {}".format(index, value)) # and amount of placeholders == amount of arguments print("Amount of placeholders: {}, Amount of variables: {}".format(message.count('{}'), len(arguments))) # But this still fails! Why? print(message

Technique on how to format/color NSTextView's string

那年仲夏 提交于 2019-12-05 05:35:20
I'm looking for a reliable technique to do simple string formatting(bold, italic,...) in a NSTextView. The text parsing is almost done with regex, but now I need to apply font trait and also change the size. Some code snippets on how I make a text bold [[textView textStorage] beginEditing]; [[textView textStorage] applyFontTraits:NSFontBoldTrait range:range]; [[textView textStorage] endEditing]; This and also the size changes with [[textView textStorage] beginEditing]; NSFont* font = [[textView textStorage] attribute:NSFontAttributeName atIndex:range.location effectiveRange:nil]; NSFont*

How to create a variadic template string formatter

懵懂的女人 提交于 2019-12-05 05:18:52
We need to format strings all the time. It would be so nice to be able to say: std::string formattedStr = format("%s_%06d.dat", "myfile", 18); // myfile_000018.dat Is there a C++ way of doing this? Some alternatives I considered: snprintf : uses raw char buffers. Not nice in modern C++ code. std::stringstream : does not support format pattern strings, instead you must push clumsy iomanip objects into the stream. boost::format : uses an ad-hoc operator overload of % to specify the arguments. Ugly. Isn't there a better way with variadic templates now that we have C++11? It can certainly be

python: extracting variables from string templates

萝らか妹 提交于 2019-12-05 05:12:32
I am familiar with the ability to insert variables into a string using Templates , like this: Template('value is between $min and $max').substitute(min=5, max=10) What I now want to know is if it is possible to do the reverse. I want to take a string, and extract the values from it using a template, so that I have some data structure (preferably just named variables, but a dict is fine) that contains the extracted values. For example: >>> string = 'value is between 5 and 10' >>> d = Backwards_template('value is between $min and $max').extract(string) >>> print d {'min': '5', 'max':'10'} Is