Is there a format specification option to display null values as empty, otherwise use a format?
>>> from decimal import Decimal >>> '{:+010,.2f}'.format(Decimal('1234.56')) '+01,234.56' >>> '{:???f}'.format(Decimal(0)) '' >>>
UPDATE:
I need the same behavior as here:
http://msdn.microsoft.com/en-us/library/0c899ak8.aspx#SectionSeparator
If Python does not have it in standard libraries, please confirm this and I will accept it as an answer.
Python , , . .
:
print(format(a, '+010,.2f') if a else "")
formatdoes a lot of things, but that’s not what it is for. There is also a very simple solution:
format
if a == 0: print("") else: print(format(a, '+010,.2f'))
if a: print(...)
How to define a function:
def format_cond(val,fmt,cond=bool,otherwise=''): return format(val, fmt) if cond(val) else otherwise
Source: https://habr.com/ru/post/1780460/More articles:Windows package - How to get external IP address in batch file variable - windowsHow do you handle sorting, swap, and filtering options? - sortingPOSIX Threads and SIGSEGV - pthreadsassociate nested attributes with identifiers that they get after saving - ruby-on-railsThe default value for textarea is htmlASP.NET MVC - How to set the default value in a strongly typed text article? - asp.net-mvc-2Interview Question how to prevent n threads from n deadlock resources in java - multithreadingHow to get specific lines from a text file in C #? - stringМожно ли преобразовать TracQuery в оператор Trac SQL и наоборот? - sqlAnalysis error in a simple Haskell Fibonacci implementation - haskellAll Articles