string-formatting

Format string in python with variable formatting

浪尽此生 提交于 2020-01-01 04:52:04
问题 How can I use variables to format my variables? cart = {"pinapple": 1, "towel": 4, "lube": 1} column_width = max(len(item) for item in items) for item, qty in cart.items(): print "{:column_width}: {}".format(item, qty) > ValueError: Invalid conversion specification or (...): print "{:"+str(column_width)+"}: {}".format(item, qty) > ValueError: Single '}' encountered in format string What I can do, though, is first construct the formatting string and then format it: (...): formatter = "{:"+str

prevent rounding of decimals when using currency string format

那年仲夏 提交于 2019-12-30 23:52:44
问题 I have some decimal data coming from an external service. I need to format the data to 2 decimal places as it is represents money, but if I use the standard C format, I rounds the number: var x = 42.999m; var y = string.Format("{0:C}", x); This results in y containing £43.00. How can I have it round down to £42.99? (Note that this question is not the same) 回答1: If you want to use a non-default rounding strategy, you'd need to do something like: var x = 42.999m; var y = string.Format("{0:C}",

Pad or truncate string based on fixed length

我的梦境 提交于 2019-12-30 10:07:45
问题 Currently have code that looks something like; print '{: <5}'.format('test') This will pad my string with ' ' if it is less than 5 characters. If the string is more than 5 characters, I'd need the string to be truncated. Without explicitly checking the length of my string before formatting it, is there a better way to pad if less than fixed length or truncate if greater than fixed length? 回答1: You can use 5.5 to combine truncating and padding so that the output will always be of length of

Indenting Paragraph With cout

最后都变了- 提交于 2019-12-30 08:34:08
问题 Given a string of unknown length, how can you output it using cout so that the entire string displays as an indented block of text on the console? (so that even if the string wraps to a new line, the second line would have the same level of indentation) Example: cout << "This is a short string that isn't indented." << endl; cout << /* Indenting Magic */ << "This is a very long string that will wrap to the next line because it is a very long string that will wrap to the next line..." << endl;

Indenting Paragraph With cout

十年热恋 提交于 2019-12-30 08:34:06
问题 Given a string of unknown length, how can you output it using cout so that the entire string displays as an indented block of text on the console? (so that even if the string wraps to a new line, the second line would have the same level of indentation) Example: cout << "This is a short string that isn't indented." << endl; cout << /* Indenting Magic */ << "This is a very long string that will wrap to the next line because it is a very long string that will wrap to the next line..." << endl;

Rounding decimals in nested data structures in Python

允我心安 提交于 2019-12-30 08:25:13
问题 I have a program which deals with nested data structures where the underlying type usually ends up being a decimal. e.g. x={'a':[1.05600000001,2.34581736481,[1.1111111112,9.999990111111]],...} Is there a simple pythonic way to print such a variable but rounding all floats to (say) 3dp and not assuming a particular configuration of lists and dictionaries? e.g. {'a':[1.056,2.346,[1.111,10.000],...} I'm thinking something like pformat(x,round=3) or maybe pformat(x,conversions={'float':lambda x:

python logging string formatting

六眼飞鱼酱① 提交于 2019-12-30 06:06:28
问题 I am using python's log formatter to format log records and i have a fmt value of fmt = "[%(filename)s:%(lineno)s] %(message)s" What i would like is that "[file.py:20]" to be stretched to 10 characters wide (for example). If it was one value that would have been easy but is there any way to stretch this entire structure to a specified length? I want something like: tmp = "[%(filename)s:%(lineno)s]" fmt = "%(tmp)10s %(message)s" I would like to know if this is possible using string formatting

python logging string formatting

跟風遠走 提交于 2019-12-30 06:05:10
问题 I am using python's log formatter to format log records and i have a fmt value of fmt = "[%(filename)s:%(lineno)s] %(message)s" What i would like is that "[file.py:20]" to be stretched to 10 characters wide (for example). If it was one value that would have been easy but is there any way to stretch this entire structure to a specified length? I want something like: tmp = "[%(filename)s:%(lineno)s]" fmt = "%(tmp)10s %(message)s" I would like to know if this is possible using string formatting

How to have HTML New Line Feature in Android Text View?

梦想的初衷 提交于 2019-12-30 05:43:52
问题 I am using webservice in my android application. In my webservice result I get a HTML formatted String as result. Assume this is my web service result: Books on Chennai :\n Madras Discovered, Tales of Old and New Madras, Madras (1992) by S. Muthiah \n Madras – its Past and its Present (1995) by S. Muthiah \n Madras – its Yesterdays and Todays and Tomorrows by S. Muthiah \n At Home in Madras (1989) by S. Muthiah \n The Spirit of Chepauk (1998) by S. Muthiah \n The Story Of Fort St. George

Python string formatting when string contains “%s” without escaping

那年仲夏 提交于 2019-12-30 02:47:12
问题 When formatting a string, my string may contain a modulo "%" that I do not wish to have converted. I can escape the string and change each "%" to "%%" as a workaround. e.g., 'Day old bread, 50%% sale %s' % 'today!' output: 'Day old bread, 50% sale today' But are there any alternatives to escaping? I was hoping that using a dict would make it so Python would ignore any non-keyword conversions. e.g., 'Day old bread, 50% sale %(when)s' % {'when': 'today'} but Python still sees the first modulo %