python对于中文编码处理的几种方式
2016-05-12 16:30
597 查看
我们在编写程序或者脚本的时候,会遇到“中文”编码的问题,导致整个程序无法正常运行,古整理了目前知道的所有“python对于中文编码处理的几种方式”,具体如下:
#coding=utf-8 #如何中文输出'python的中文编码方式' #方法一:头部加 【# -*- coding: utf-8 -*-】 n = 'python的中文编码方式111111' print n #方法二:头部加 【#coding=utf-8】 print 'python的中文编码方式222222' #方法三:在文本前加一个 u print u'python的中文编码方式333333' #方法四:调用encode()方法 import sys reload(sys) sys.setdefaultencoding('utf-8') print n.encode('utf-8') + '4444444' #方法五:强制类型转换,使用unicode() print unicode('python的中文编码方式5555555') #方法六:强制类型转换,使用unicode(s , encoding = '') list = ['你好:' ,'python!'] print unicode(list[0] , encoding='utf-8') + unicode(list[1] , encoding='utf-8') ''' 需要注意的是utf-8并不是万能的,我们需要保持脚本、浏览器、程序三者编码之 间的转换;如果utf-8不能解决,可以尝试GBK 或修改浏览器的默认编码。 '''
相关文章推荐
- Python动态类型的学习---引用的理解
- Python3写爬虫(四)多线程实现数据爬取
- 垃圾邮件过滤器 python简单实现
- 下载并遍历 names.txt 文件,输出长度最长的回文人名。
- install and upgrade scrapy
- Scrapy的架构介绍
- Centos6 编译安装Python
- 使用Python生成Excel格式的图片
- 让Python文件也可以当bat文件运行
- [Python]推算数独
- Python中zip()函数用法举例
- Python中map()函数浅析
- Python将excel导入到mysql中
- Python在CAM软件Genesis2000中的应用
- 使用Shiboken为C++和Qt库创建Python绑定
- FREEBASIC 编译可被python调用的dll函数示例
- Python 七步捉虫法