中文unicode 中文str 读写数据 读写文件

POST数据,GET数据,数据库查询数据(设置了编码),json转dict数据都是unicode字符串。

mysqldb

注:设置charset='utf8'
1.中文 str     能正常写入  
2.中文 unicode 能正常写入

1.英文 str     能正常写入
2.英文 unicode 能正常写入

1.中文 str|unicode 和 英文str 同时存在 能正常写入
2.中文 str 和 中文unicode 同时存在 报错
3.中文 str 和 英文unicode 同时存在 报错

1.查询 unicode

注:未设置charset='utf8'
1.中文 str     能正常写入
2.中文 unicode 报错
3.中文 str unicode 同时存在 报错


1.英文 str     能正常写入
2.英文 unicode 报错

2.查询 str


数据库

数据库入库时候charset = 编码 ,就可以直接插入unicode字符串入库。查询出来 和 POST过来的数据 都是unicode字符,设置编码 ,可以直接让unicode中文入库。

conn=MySQLdb.connect(host='localhost',user='root',passwd='',db='azheapp',port=3306,charset="utf8",cursorclass = MySQLdb.cursors.DictCursor)


文件

f = codecs.open('test.txt','r+','utf8');  就可以直接插入unicode字符串写入文件。

用普通open,只能插入str字符串


django.db.connection
太强大了
不管字符串 是str还是unicode 或者同时存在 都能插入数据库



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值