string-formatting

Can Python's logging format be modified depending on the message log level?

痴心易碎 提交于 2019-11-29 21:12:34
I'm using Python's logging mechanism to print output to the screen. I could do this with print statements, but I want to allow a finer-tuned granularity for the user to disable certain types of output. I like the format printed for errors, but would prefer a simpler format when the output level is "info." For example: logger.error("Running cmd failed") logger.info("Running cmd passed") In this example, I would like the format of the error to be printed differently: # error Aug 27, 2009 - ERROR: Running cmd failed # info Running cmd passed Is it possible to have different formats for different

Linq-to-Entities: Format Date in select query expression

只谈情不闲聊 提交于 2019-11-29 21:06:24
问题 I am trying to get a formatted date string directly from a LINQ-to-Entities query expression. nonBusinessDays = (from ac in db.AdminCalendar where ac.DateTimeValue >= calendarStartDate && ac.DateTimeValue <= calendarEndDate && ac.IsBusinessDay == false select ac.MonthValue + "/" + ac.DayOfMonth + "/" + ac.FullYear).ToList(); But, I get the folloinw error message: "Unable to cast the type 'System.Nullable`1' to type 'System.Object'. LINQ to Entities only supports casting Entity Data Model

Python, print all floats to 2 decimal places in output

╄→гoц情女王★ 提交于 2019-11-29 20:50:40
I need to output 4 different floats to two decimal places. This is what I have: print '%.2f' % var1,'kg =','%.2f' % var2,'lb =','%.2f' % var3,'gal =','%.2f' % var4,'l' Which is very unclean, and looks bad. Is there a way to make any float in that out put '%.2f'? Note: Using Python 2.6. Well I would atleast clean it up as follows: print "%.2f kg = %.2f lb = %.2f gal = %.2f l" % (var1, var2, var3, var4) If you just want to convert the values to nice looking strings do the following: twodecimals = ["%.2f" % v for v in vars] Alternatively, you could also print out the units like you have in your

Why use endl when I can use a newline character? [duplicate]

扶醉桌前 提交于 2019-11-29 19:40:11
This question already has an answer here: C++: “std::endl” vs “\n” 13 answers Is there a reason to use endl with cout when I can just use \n ? My C++ book says to use endl, but I don't see why. Is \n not supported as widely as endl , or am I missing something? endl appends '\n' to the stream and calls flush() on the stream. So cout << x << endl; is equivalent to cout << x << '\n'; cout.flush(); A stream may use an internal buffer which gets actually streamed when the stream is flushed. In case of cout you may not notice the difference since it's somehow synchronized ( tied ) with cin , but for

TypeError: not enough arguments for format string when using %s

≯℡__Kan透↙ 提交于 2019-11-29 18:40:32
问题 this is my code import sys name = input("Enter your name:") last_name = input("Enter your last name:") gender = input("Enter your gender:") age = input("Enter your age:") print ("So your name is %s, your last name is %s, you are %s and you are %s years old" % name, last_name, gender, age) I've searched the topic but I don't understand. 回答1: You need to put your arguments for string formatting in parenthesis: print (... % (name, last_name, gender, age)) Otherwise, Python will only see name as

What does {0} mean in String.Format?

余生颓废 提交于 2019-11-29 18:25:49
E.g. in the following example: string commandText = string.Format("Select * from {0}", filename); How does the above work? {0} is a placeholder for the first object given; in this case that's filename , so it will insert whatever filename evaluates to in place of {0} . Similarly, of course you could use {1} and that would be replaced with the second parameter passed, etc. {0} refers to the second parameter passed into String.Format . {1} refers to the third, {2} to the fourth, etc. For example: String.Format("The {0} brown {1} jumps {2} the {3} dog.", "quick", "fox", "over", "lazy") Evaluates

Format a string that has extra curly braces in it

最后都变了- 提交于 2019-11-29 17:08:21
问题 I have a LaTeX file I want to read in with Python 3 and format a value into the resultant string. Something like: ... \textbf{REPLACE VALUE HERE} ... But I have not been able to figure out how to do this since the new way of doing string formatting uses {val} notation and since it is a LaTeX document, there are tons of extra {} characters. I've tried something like: '\textbf{This and that} plus \textbf{{val}}'.format(val='6') but I get KeyError: 'This and that' 回答1: Method 1, which is what I

What does bb or bbbb represent in an EN-US TEXT function or Custom Number format mask?

末鹿安然 提交于 2019-11-29 16:52:14
On a EN-US regional system, what does B or b represent in a format mask? While it does nothing in VBA's Format function , both the TEXT function and Custom Number Formatting recognize b as a legal Number Format Code and return a number that I cannot determine the origin of. To further confuse matters, larger numbers return different values when formatted with bbbb and while blocks of sequential numbers return the same value, there are transitions when they jump ahead a digit. I do know that Excel expects the format mask to be either bb or bbbb because if B or b is supplied as a custom number

StringFormat on Binding

只谈情不闲聊 提交于 2019-11-29 16:39:55
问题 View: <TextBlock Text="{Binding Date}"/> I want to format the Date to "dd/MM/yyyy", in other words, without the time. I tried it: <TextBlock Text="{Binding Date, StringFormat={}{0:dd/MM/yyyy}}"/> , but it doesn't work. Gives me an error: The property 'StringFormat' was not found in type 'Binding'. 回答1: The best and the easiest way would be to use a converter to which you pass the Date and get the formatted string back. In e.g. MyNamespace.Converters namespace: public class DateFormatConverter

Text formatting error: '=' alignment not allowed in string format specifier

女生的网名这么多〃 提交于 2019-11-29 16:04:55
问题 What does '=' alignment mean in the following error message, and why does this code cause it? >>> "{num:03}".format(num="1") Traceback (most recent call last): File "<stdin>", line 1, in <module> ValueError: '=' alignment not allowed in string format specifier The code has a subtle problem: the input value "1" is text, not a number. But the error message doesn't appear to have anything to do with that. Nothing in the error message indicates why “'=' alignment” is relevant, and it does not