string-formatting

ValueError: cannot switch from manual field specification to automatic field numbering

别等时光非礼了梦想. 提交于 2019-11-30 11:50:41
The class: class Book(object): def __init__(self, title, author): self.title = title self.author = author def get_entry(self): return "{0} by {1} on {}".format(self.title, self.author, self.press) Create an instance of my book from it: In [72]: mybook = Book('HTML','Lee') In [75]: mybook.title Out[75]: 'HTML' In [76]: mybook.author Out[76]: 'Lee' Please notice that I didn't initialize attribute 'self.press',while use it in the get_entry method.Go ahead to type in data. mybook.press = 'Murach' mybook.price = 'download' Till now, I can specify all the data input with vars In [77]: vars(mybook)

Format a string that has extra curly braces in it

蓝咒 提交于 2019-11-30 11:26:22
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' Method 1, which is what I'd actually do: use a string.Template instead. >>> from string import Template >>> Template(r'\textbf{This

StringFormat on Binding

痴心易碎 提交于 2019-11-30 11:25:06
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'. CodeNoob 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 : IValueConverter { public object Convert(object value, Type targetType, object parameter, string

How should I properly use __attribute__ ((format (printf, x, y))) inside a class method in C++?

眉间皱痕 提交于 2019-11-30 11:02:21
I'm trying to define a class method for debug prints that will behave like printf : inline void debug(const char* fmt, ...) __attribute__ ((format (printf, 1, 2))) This complains about: error: format string argument not a string type I recalled that a class method declaration has an implicit this parameter, so I changed the locations of the parameters to 2, 3: inline void debug(const char* fmt, ...) __attribute__ ((format (printf, 2, 3))) and now it compiles, but it looks like the parameters are shifted, as if the this parameter were being treated as part of the argument list. How can I tell

Can I format NULL values in string.Format?

那年仲夏 提交于 2019-11-30 10:44:26
I was wondering if there's a syntax for formatting NULL values in string.Format, such as what Excel uses For example, using Excel I could specify a format value of {0:#,000.00;-#,000.00,NULL} , which means display the numeric value as number format if positive, number format in parenthesis if negative, or NULL if the value is null string.Format("${0:#,000.00;(#,000.00);NULL}", someNumericValue); Edit I'm looking for formatting NULL / Nothing values for all data types, not just numeric ones. My example is actually incorrect because I mistakenly thought Excel used the 3rd parameter if the value

In C#, what is the best method to format a string as XML?

我与影子孤独终老i 提交于 2019-11-30 10:44:23
I am creating a lightweight editor in C# and would like to know the best method for converting a string into a nicely formatted XML string. I would hope that there's a public method in the C# library like "public bool FormatAsXml(string text, out string formattedXmlText)", but it couldn't be that easy, could it? Very specifically, what would the method "SomeMethod" have to be that would produce the output below? string unformattedXml; string formattedXml; unformattedXml = "<?xml version=\"1.0\"?><book><author>Lewis, C.S.</author><title>The Four Loves</title></book>" formattedXml = SomeMethod

What does {0} mean in String.Format?

不问归期 提交于 2019-11-30 10:10:13
问题 E.g. in the following example: string commandText = string.Format("Select * from {0}", filename); How does the above work? 回答1: {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. 回答2: {0} refers to the second parameter passed into String.Format . {1} refers to the third, {2} to the fourth, etc.

In PHP, how do I add to a zero-padded numeric string and preserve the zero padding?

跟風遠走 提交于 2019-11-30 09:04:53
If I have a variable in PHP containing 0001 and I add 1 to it, the result is 2 instead of 0002 . How do I solve this problem? $foo = sprintf('%04d', $foo + 1); dirtside It would probably help you to understand the PHP data types and how they're affected when you do operations to variables of various types. You say you have "a variable in PHP say 0001", but what type is that variable? Probably a string, "0001", since an integer can't have that value (it's just 1). So when you do this: echo ("0001" + 1); ...the + operator says, "Hm, that's a string and an integer. I don't know how to add a

StringFormat on Combobox Displaymemberpath

ⅰ亾dé卋堺 提交于 2019-11-30 08:21:57
I am trying to use StringFormat on DisplayMemberPath property of a ComboBox (WPF). But i don't know even if this is possible. can someone help me with some ideas. I am trying to do something like this: <ComboBox DisplayMemberPath="{Binding Path=MyDateField, StringFormat={}{0:dd/MM/yyyy}}" Name="CmbName" Width="120" /> But it isn't working... Thx All simply use the ItemStringFormat property (works only if IsEditable="False") <ComboBox ItemsSource="{Binding YourItems}" DisplayMemberPath="MyDateField" ItemStringFormat="{}{0:dd/MM/yyyy}" /> hope this helps 来源: https://stackoverflow.com/questions

The correct Javascript Date.parse(…) format string?

做~自己de王妃 提交于 2019-11-30 08:20:48
What is a culture-invariant way of constructing a string such that the Javascript Date() constructor can parse it and create the proper date object? I have tried these format strings which don't work (using C# to generate the strings): clientDate.ToString(); // gives: "11/05/2009 17:35:23 +00:00" clientDate.ToString("MMM' 'dd', 'yyyy' 'h':'mm':'ss' 'tt"); // works on an English server // but on a French server, gives: "mai 11, 2009 5:35:23" // Javascript won't parse that. clientDate.ToString("MM'-'dd'-'yyyy' 'HH':'mm':'ss") // gives: 05-11-2009 17:35:23 What is the universal format?? Maciej