string.format

Use of '.format()' vs. '%s' in cursor.execute() for mysql JSON field, with Python mysql.connector,

那年仲夏 提交于 2021-02-18 16:59:53
问题 My objective is to store a JSON object into a MySQL database field of type json, using the mysql.connector library. import mysql.connector import json jsonData = json.dumps(origin_of_jsonData) cnx = mysql.connector.connect(**config_defined_elsewhere) cursor = cnx.cursor() cursor.execute('CREATE DATABASE dataBase') cnx.database = 'dataBase' cursor = cnx.cursor() cursor.execute('CREATE TABLE table (id_field INT NOT NULL, json_data_field JSON NOT NULL, PRIMARY KEY (id_field))') Now, the code

Make String.format(“%s”, arg) display null-valued arguments differently from “null”

*爱你&永不变心* 提交于 2020-05-10 03:39:49
问题 Consider the custom toString() implementation of a bean: @Override public String toString() { String.format("this is %s", this.someField); } This yields this is null if someField is null. Is there a way to override the default null string representation of null-valued arguments to another text, i.e., ? without calling explicitly replaceAll(...) in the toString method? Note : The bean inherits from a superclass that could implement Formattable (http://docs.oracle.com/javase/7/docs/api/java

How to print the list in 'for' loop using .format() in Python?

谁都会走 提交于 2020-03-14 05:18:12
问题 I'm a newbie to Python. I'm writing a very simple piece of code to print the contents of a list using 'for' loop with .format() and I want the output as below, but I'm getting this error: names = ['David', 'Peter', 'Michael', 'John', 'Bob'] for i in names: print("{}.{}".format(i, names[i])) print("{}.{}".format(i,breakfastMenu[i])) TypeError: list indices must be integers or slices, not str Expected output I want: 1. David 2. Peter 3. Michael 4. John 5. Bob Can someone please help me to get

How to print the list in 'for' loop using .format() in Python?

爷,独闯天下 提交于 2020-03-14 05:17:09
问题 I'm a newbie to Python. I'm writing a very simple piece of code to print the contents of a list using 'for' loop with .format() and I want the output as below, but I'm getting this error: names = ['David', 'Peter', 'Michael', 'John', 'Bob'] for i in names: print("{}.{}".format(i, names[i])) print("{}.{}".format(i,breakfastMenu[i])) TypeError: list indices must be integers or slices, not str Expected output I want: 1. David 2. Peter 3. Michael 4. John 5. Bob Can someone please help me to get

Get number of placeholders in Formatter.format() String

早过忘川 提交于 2020-02-04 01:28:15
问题 In a Java program I am using a String with Formatter.format() function, which I get from server. I cannot be sure that String to be formatted has placeholder or a valid number of them. If it the String is not as expected I would like to throw an exception - or log it somehow. At this point I don't care of what type of placeholders are ( String , Integer ,...), I would just like to get number of expected parameters for each String. What is the easiest way to achieve this? One way could be to

Interpreting dates: Console.Writeline vs. string.Format

守給你的承諾、 提交于 2020-01-13 09:49:05
问题 Given the following C# code: var dt = DateTime.Now; Console.WriteLine("{0:MM/dd/yy} ... {1}", dt, string.Format("{0:MM/dd/yy}", dt)); ... when the short date (under Windows 7, Control Panel -> Region and Language -> Additonal Settings -> Date ) is set to the USA standard of " M/d/yyyy ," I get this: 06/17/14 ... 06/17/14 However, when I change the short date to " ddd dd MMM yyyy ," I get this: 06/17/14 ... 06 17 14 I was under the impression that Console.WriteLine and string.Format always

Localization of singular/plural words - what are the different language rules for grammatical numbers?

不羁的心 提交于 2020-01-12 03:14:27
问题 I have been developing a .NET string formatting library to assist with localization of an application. It's called SmartFormat and is open-source on GitHub. One of the issues it tries to address is Grammatical Numbers. This is also known as "singular and plural forms" or "conditional formatting", and here's a snippet of what it looks like in English: var message = "There {0:is|are} {0} {0:item|items} remaining"; // You can use the Smart.Format method just like using String.Format: var output

Exception while using String.Format “Index (zero based) must be greater than or equal to zero and less than the size of the argument list.”

做~自己de王妃 提交于 2020-01-03 00:56:27
问题 I have an array ArrayList array = new ArrayList(); array.Add("a"); array.Add("b"); array.Add("c"); and I have a string variable refFormat which has the format as below. string refFormat = "{2} {0}"; I'm trying to get a string of values from the array with this format. Below is what I have written. string newStr = String.Format(refFormat,array.ToArray()); I'm getting the following exception when I'm trying to do this. Index (zero based) must be greater than or equal to zero and less than the

String.Format vs ToString()

好久不见. 提交于 2020-01-01 08:01:06
问题 Can anyone explain if there is any benefit in either one of the following methods: decimal d = 12.0m; // 1. how I'd have done it myLabel.Text = d.ToString(); // 2. how I saw someone do it today myLabel.Text = String.Format("{0}", d); Just to clarify, I'm not querying what the methods do, I'm obviously happy with that, just if there is perhaps a performance benefit in one over the other in this specific example. I'm aware of the added flexibility of cultures and formatting offered by string

String.Format vs ToString()

∥☆過路亽.° 提交于 2020-01-01 08:00:07
问题 Can anyone explain if there is any benefit in either one of the following methods: decimal d = 12.0m; // 1. how I'd have done it myLabel.Text = d.ToString(); // 2. how I saw someone do it today myLabel.Text = String.Format("{0}", d); Just to clarify, I'm not querying what the methods do, I'm obviously happy with that, just if there is perhaps a performance benefit in one over the other in this specific example. I'm aware of the added flexibility of cultures and formatting offered by string