python unicode support

关于字符编码 有几个好的文章 哈哈 programmer必备基础知识~~

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])+"')")
	

    









  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值