python format函数

通过{}:来代替%。字符串的format函数可接受不限参数,位置可以不按顺序,可以不用或者用多次,不过2.6不能为空{},2.7才可以。传list/tuple/dict给format函数,非常方便。

通过位置:

>>> '{},{}'.format('dora',17)

'dora,17'

>>> '{0},{1}'.format('dora',17)

'dora,17'

>>> '{1},{0},{1}'.format('dora',17)

'17,dora,17'

通过关键字:

>>> '{name},{age}'.format(name='dora',age=17)

'dora,17'

通过对象属性:

>>> class Person:

... def __init__(self,name,age):

... self.name,self.age = name,age

... def __str__(self):

... print 'This guy is {self.name},is {self.age} old'.format(self=self)

通过下标:

>>> p = ['dora', 17]

>>> '{0[1]},{0[0]}'.format(p)

'17,dora'

格式限定符

语法是{}中带:号,填充常跟对齐一起使用
^、<、>分别是居中、左对齐、右对齐,后面带宽度
:号后面带填充的字符,只能是一个字符,默认是用空格填充

>>> '{:>8}'.format(17)

' 17'

>>> '{:0>8}'.format(17)

'00000017'

>>> '{:0a>8}'.format(17)

>>> >>> '{:a>8}'.format(17)

'aaaaaa17'

精度常跟类型f一起使用

>>> '{:.2f}'.format(17.1234567)

'17.12'

b、d、o、x分别是二进制、十进制、八进制、十六进制

>>> '{:b}'.format(17)

'10001'

>>> '{:d}'.format(17)

'17'

>>> '{:o}'.format(17)

'21'

>>> '{:x}'.format(17)

'11'

用,号金额的千位分隔符

>>> '{:,}'.format(171234567)

'171,234,567'

MySQL tried to insert Chinese characters into table fields, Error Code: 1366. Incorrect string value

Error Code: 1366. Incorrect string value.

query related character set,

mysql > show variables like ‘%char%’;

+——————————————————–+

| Variablename | Value |

+————————–+——————————————————–+

| charactersetclient | utf8 |

| charactersetconnection | utf8 |

| charactersetdatabase | latin1 |

| charactersetfilesystem | binary |

| charactersetresults | utf8 |

| charactersetserver | latin1 |

| charactersetsystem | utf8 |

| charactersetsdir | /usr/local/mysql-5.6.25-osx10.8-x8664/share/charsets/ |

+————————–

Find out fields’ attributes of the table:

show full columns from tb.orderinfo;

If you want to change the table default character set and all character columns to a new character set, use a statement like this, so the query statement will be:

ALTER TABLE tb.orderinfo CONVERT TO CHARACTER SET utf8;

The collation is converted to ‘utf8’.