http://www.joelonsoftware.com/articles/Unicode.html
http://www.ruanyifeng.com/blog/2007/10/ascii_unicode_and_utf-8.html
http://wklken.me/posts/2013/08/31/python-extra-coding-intro.html
然后以下部分是对python how to unicode doc的学习理解~~
python里面unicode string 是unicode类型的实例,这个unicode类型是python内建的类型~~
basestring是 str这个数据类型的祖先。
unicode()构造函数有这样的签名 unicode(string[, encoding, erroes]) 所有的参数必须是8-bit的string
参数1 需要encoding的数据 ;
参数2 encoding 的type utf-8等;
参数3 错误 参数有 'strick' 'replace' 'ignore'这三种
examples:
one-character unicode string 也可以通过unichr()这个内置的函数创建
unichr()这个函数接受整数参数,然后返回1个长度为1的unicode string
unicode类型的数据也有一个操作函数
s = u'Was ever so'
s.count('e')
s.find()
s.replace('','')
s.upper()
以上函数的参数可以是unicode 格式的string 或是 8-bit的string 。其实8-bit的string在被移入操作之前,还是会被转换成unicode string的
python默认的编码格式是ASCII编码 所以呢长度超过字符长度超过127就会出错
还有一个比较重要函数就是.encode([encoding], [error='strick'])
这个函数返回8-bit unicode string
.decode([encoding], [errors])这个函数用给定的格式解释string
# -*- coding: utf-8 -*-
import sqlite3 as lite
import sys
import xlrd
def convert2uni(arg):
if isinstance(arg, float):
return str(arg).encode('utf-8')
if isinstance(arg, str):
return arg.encode('utf-8')
if isinstance(arg, unicode):
return arg
pass
#连接cexcel
fname = "haihui.xls"
book = xlrd.open_workbook(fname)
sh = book.sheet_by_index(0)
#sheet名称 行 列
print sh.name, sh.nrows, sh.ncols
#创建DB 创建table
con = lite.connect('haihui.db')
with con:
cur = con.cursor()
cur.execute("drop table if exists students")
cur.execute('create table students(rowid INTEGER PRIMARY KEY AUTOINCREMENT ,sid int ,name int,plong int, pshort int)')
i = 0;
for rx in range(sh.nrows):
i = i+1
row = sh.row_values(rx)
print i
if row:
cur.execute("insert into students (sid,name,plong,pshort) values('"+convert2uni(row[0])+"','" +convert2uni(row[1])+"','" +convert2uni(row[3])+"','" +convert2uni(row[5])+"')")