string-formatting

Python TypeError: non-empty format string passed to object.__format__

邮差的信 提交于 2019-11-26 17:44:08
I hit this TypeError exception recently, which I found very difficult to debug. I eventually reduced it to this small test case: >>> "{:20}".format(b"hi") Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: non-empty format string passed to object.__format__ This is very non-obvious, to me anyway. The workaround for my code was to decode the byte string into unicode: >>> "{:20}".format(b"hi".decode("ascii")) 'hi ' What is the meaning of this exception? Is there a way it can be made more clear? bytes objects do not have a __format__ method of their own, so the

How can CString be passed to format string %s?

怎甘沉沦 提交于 2019-11-26 17:23:13
问题 class MyString { public: MyString(const std::wstring& s2) { s = s2; } operator LPCWSTR() const { return s.c_str(); } private: std::wstring s; }; int _tmain(int argc, _TCHAR* argv[]) { MyString s = L"MyString"; CStringW cstring = L"CString"; wprintf(L"%s\n", (LPCWSTR)cstring); // Okay. Becase it has an operator LPCWSTR() wprintf(L"%s\n", cstring); // Okay, fine. But how? wprintf(L"%s\n", (LPCWSTR)s); // Okay. fine. wprintf(L"%s\n", s); // Doesn't work. Why? It prints gabage string like "?."

Correct format specifier for return value of sizeof() in C

核能气质少年 提交于 2019-11-26 17:12:00
问题 I have the following code: #include<stdio.h> int main() { printf("The 'int' datatype is \t\t %lu bytes\n", sizeof(int)); printf("The 'unsigned int' data type is\t %lu bytes\n", sizeof(unsigned int)); printf("The 'short int' data type is\t %lu bytes\n", sizeof(short int)); printf("The 'long int' data type is\t %lu bytes\n", sizeof(long int)); printf("The 'long long int' data type is %lu bytes\n", sizeof(long long int)); printf("The 'float' data type is\t %lu bytes\n", sizeof(float)); printf(

Leaving values blank if not passed in str.format

瘦欲@ 提交于 2019-11-26 16:41:30
问题 I've run into a fairly simple issue that I can't come up with an elegant solution for. I'm creating a string using str.format in a function that is passed in a dict of substitutions to use for the format. I want to create the string and format it with the values if they're passed and leave them blank otherwise. Ex kwargs = {"name": "mark"} "My name is {name} and I'm really {adjective}.".format(**kwargs) should return "My name is mark and I'm really ." instead of throwing a KeyError (Which is

Nested f-strings

喜欢而已 提交于 2019-11-26 16:38:52
问题 Thanks to David Beazley's tweet, I've recently found out that the new Python 3.6 f-strings can also be nested: >>> price = 478.23 >>> f"{f'${price:0.2f}':*>20s}" '*************$478.23' Or: >>> x = 42 >>> f'''-{f"""*{f"+{f'.{x}.'}+"}*"""}-''' '-*+.42.+*-' While I am surprised that this is possible, I am missing on how practical is that, when would nesting f-strings be useful? What use cases can this cover? Note: The PEP itself does not mention nesting f-strings, but there is a specific test

Right padding with zeros in Java

心已入冬 提交于 2019-11-26 16:33:14
问题 Sorry if this question was made already, I've made a deep search and nothing. Now, I know that: String.format("%05d", price); Will be padding my price with zeros to the left, so a price of 25 will result in 00025 What if I want to pad them to the right, so the result is 25000 ? How do I do that using only String.format patterns? 回答1: You could use: String.format("%-5s", price ).replace(' ', '0') Can I do this using only the format pattern? String.format uses Formatter.justify just like the

Plural String Formatting

谁说我不能喝 提交于 2019-11-26 16:27:19
问题 Given a dictionary of int s, I'm trying to format a string with each number, and a pluralization of the item. Sample input dict : data = {'tree': 1, 'bush': 2, 'flower': 3, 'cactus': 0} Sample output str : 'My garden has 1 tree, 2 bushes, 3 flowers, and 0 cacti' It needs to work with an arbitrary format string. The best solution I've come up with is a PluralItem class to store two attributes, n (the original value), and s (the string 's' if plural, empty string '' if not). Subclassed for

String with 'f' prefix in python-3.6

烂漫一生 提交于 2019-11-26 16:13:34
I'm trying out Python 3.6. Going through new code, I stumbled upon this new syntax: f"My formatting string!" It seems we can do things like this: >>> name = "George" >>> print(f"My cool string is called {name}.") My cool string is called George. Can anyone shed some light on the inner workings of this? In particular what is the scope of the variables that an f-prefixed string can take? Kwong Leong See PEP 498 Literal String Interpolation : The expressions that are extracted from the string are evaluated in the context where the f-string appeared. This means the expression has full access to

Converting Float to Dollars and Cents

寵の児 提交于 2019-11-26 15:39:30
问题 First of all, I have tried this post (among others): Currency formatting in Python. It has no affect on my variable. My best guess is that it is because I am using Python 3 and that was code for Python 2. (Unless I overlooked something, because I am new to Python). I want to convert a float, such as 1234.5, to a String, such as "$1,234.50". How would I go about doing this? And just in case, here is my code which compiled, but did not affect my variable: money = float(1234.5) locale.setlocale

String.Format an integer to use a thousands separator without decimal places or leading 0 for small integers

放肆的年华 提交于 2019-11-26 15:33:59
问题 Silly question, I want to format an integer so that it appears with the 1000's separator (,), but also without decimal places and without a leading 0. My attempts so far have been: String.Format("{0} {1}", 5, 5000); // 5 5000 String.Format("{0:n} {1:n}", 5, 5000); // 5.00 5,000.00 String.Format("{0:0,0} {1:0,0}", 5, 5000); // 05 5,000 The output I'm after is: 5 5,000 Is there something obvious that I'm missing? 回答1: This worked for me. String.Format("{0:#,0} {1:#,0}", 5, 5000); // 5 5,000 回答2