python decode unicode encode
字符串在Python内部的表示是unicode编码,因此,在做编码转换时,通常需要以unicode作为中间编码,即先将其他编码的字符串解码(decode)成unicode,再从unicode编码(encode)成另一种编码。
代码中字符串的默认编码与代码文件本身的编码一致,以下是不一致的两种:
1. s = u'你好'
该字符串的编码就被指定为unicode了,即python的内部编码,而与代码文件本身的编码(查看默认编码:import sys print('hello',sys.getdefaultencoding()) ascii 。设置默认编码:import sys reload(sys) sys.setdefaultencoding('utf-8')))无关。因此,对于这种情况做编码转换,只需要直接使用encode方法将其转换成指定编码即可.
2. # -*- coding: utf-8 -*-
s = ‘你好’
此时为utf-8编码,ascii编码不能显示汉字
isinstance(s, unicode) #用来判断是否为unicode ,是返回True,不是返回False
unicode(str,'gb2312')与str.decode('gb2312')是一样的,都是将gb2312编码的str转为unicode编码
使用str.__class__可以查看str的编码形式
原理说了半天,最后来个包治百病的吧:)
#!/usr/bin/env python
#coding=utf-8
s="中文"
if isinstance(s, unicode):
#s=u"中文"
print s.encode('gb2312')
else:
#s="中文"
print s.decode('utf-8').encode('gb2312')
语音模块代码:
# -*- coding: utf-8 -*-import
import sys
print('hello',sys.getdefaultencoding())
def xfs_frame_info(words): #decode utf-8 to python internal unicode coding
isinstance(words,unicode)
wordu = words.decode('utf-8') #encode python unicode to gbk
data = wordu.encode('gbk') length = len(data) + 2 frame_info = bytearray(5)
frame_info[0] = 0xfd
frame_info[1] = (length >> 8)
frame_info[2] = (length & 0x00ff)
frame_info[3] = 0x01
frame_info[4] = 0x01 buf = frame_info + data
print("buf:",buf) return buf if __name__ == "__main__": print("hello world")
words1= u'你好'
#encodetype = isinstance(words1,unicode)
#print("encodetype",encodetype)
print("origin unicode", words1) words= words1.encode('utf-8')
print("utf-8 encoded", words)
a = xfs_frame_info(words)
print('a',a) if __name__ == "__main__": print("hello world")
words1= '你好'
print("oringe utf-8 encode:",words1)
encodetype = isinstance(words1,unicode)
wordu = words1.decode('utf-8')
print("unicode from utf-8 decode:",wordu)
#encodetype = isinstance(words1,utf-8)
#encodetype = isinstance(words1,'ascii')
#print("encodetype",encodetype)
#print("origin unicode", words1) word_utf8 = wordu.encode('utf-8')
#encodetype2 = isinstance(words,utf8)
#print("encodetype2",encodetype2)
print("utf-8 encoded",word_utf8)
a = xfs_frame_info(word_utf8)
print('a',a)
你好前不加u''时,要多一步decode为unicode
python decode unicode encode的更多相关文章
- Python decode与encode
字符串在Python内部的表示是unicode编码(8-bit string),因此,在做编码转换时,通常需要以unicode作为中间编码,即先将其他编码的字符串解码(decode)成unicod ...
- 关于python decode()和 encode()
1.先收集一下这几天看到的关于decode()解码和encode()编码的用法 bytes和str是字节包和字符串,python3中会区分bytes和str,不会混用这两个.字符串可以编码成字节包,而 ...
- python decode和encode
摘抄: 字符串在Python内部的表示是Unicode编码,因此,在做编码转换时,通常需要以unicode作为中间编码,即先将其他编码的字符解码(decode)成unicode,再从unicode编码 ...
- python encode decode unicode区别及用法
decode 解码 encode 转码 unicode是一种编码,具体可以百度搜 # coding: UTF-8 u = u'汉' print repr(u) # u'\u6c49' s = u.en ...
- Python字符串的encode与decode研究心得乱码问题解决方法
为什么Python使用过程中会出现各式各样的乱码问题,明明是中文字符却显示成“\xe4\xb8\xad\xe6\x96\x87”的形式? 为什么会报错“UnicodeEncodeError: 'asc ...
- python字符decode与encode的问题
同事在工作中遇到一个字符编码的问题:问题是:从mysql数据库中读出来的varchar类型数据在python是unicode类型的. 但他却对这个unicode字符进行了decode,因为他以为读出来 ...
- Python字符串的encode与decode研究心得 乱码问题解决方法
以下摘自:http://www.jb51.net/article/17560.htm 为什么Python使用过程中会出现各式各样的乱码问题,明明是中文字符却显示成“\xe4\xb8\xad\xe6\x ...
- Python字符串的encode与decode研究心得——解决乱码问题
转~Python字符串的encode与decode研究心得——解决乱码问题 为什么Python使用过程中会出现各式各样的乱码问题,明明是中文字符却显示成“/xe4/xb8/xad/xe6/x96/x8 ...
- 【Python】关于decode和encode
#-*-coding:utf-8 import sys ''' *首先要搞清楚,字符串在Python内部的表示是unicode编码,因此,在做编码转换时,通常需要以unicode作为中间编码, 即先将 ...
随机推荐
- 牛客网NOIP赛前集训营-普及组(第二场)和 牛客网NOIP赛前集训营-提高组(第二场)解题报告
目录 牛客网NOIP赛前集训营-普及组(第二场) A 你好诶加币 B 最后一次 C 选择颜色 D 合法括号序列 牛客网NOIP赛前集训营-提高组(第二场) A 方差 B 分糖果 C 集合划分 牛客网N ...
- [LUOGU] P1111 修复公路
题目背景 A地区在地震过后,连接所有村庄的公路都造成了损坏而无法通车.政府派人修复这些公路. 题目描述 给出A地区的村庄数N,和公路数M,公路是双向的.并告诉你每条公路的连着哪两个村庄,并告诉你什么时 ...
- kvm虚拟化存储管理
1. kvm虚拟化存储介绍 KVM 的存储虚拟化是通过存储池(Storage Pool)和卷(Volume)来管理的.Storage Pool 是宿主机上可以看到的一片存储空间,可以是多种型: Vol ...
- zabbix:告警、恢复消息次数
之前zabbix配置告警,存在告警信息发送多次并且恢复信息也跟着发送多次了,导致企业微信流量不够用,没有找到恢复信息单独的设置项 动作中的步骤我个人理解为:1-5的意思是发送5条告警消息 3 ...
- modelsim安装调试
modelsim,debug:“unable to checkout a viewer license necessary for use of the modelsim graphical user ...
- perl学习之文件句柄filehandle
句柄handle 分为文件句柄和目录句柄,文件句柄实际上包含文件,进程和套接字的读写. 文件句柄的操作步骤 open(FD,"filename"); @lines=<FD ...
- php redis通用类
<?php /** * redis操作类 * 说明,任何为false的串,存在redis中都是空串. * 只有在key不存在时,才会返回false. * 这点可用于防止缓存穿透 * */ cla ...
- python基础学习笔记——os模块
#OS模块 #os模块就是对操作系统进行操作,使用该模块必须先导入模块: import os #getcwd() 获取当前工作目录(当前工作目录默认都是当前文件所在的文件夹) result = os. ...
- SQL server 事务实例
简单的SQLserver事务实例: 执行SQL 组合操作A.操作B,只有AB都执行成功时才提交事务,否则回滚事务. 测试数据表: --1.数据表A CREATE TABLE A( A1 VARCHAR ...
- 【LeetCode】Grid Illumination(网格照明)
这道题是LeetCode里的第1001道题. 题目要求: 在 N x N 的网格上,每个单元格 (x, y) 上都有一盏灯,其中 0 <= x < N 且 0 <= y < N ...