Python字符串编码
在Python中有些特殊的地方是存在两种字符串,分别为str和unicode字符串,他们都继承自basestring。
如:s="hello world",s为str;us=u"hello world",us为unicode。
使用help(str)和help(unicode)可以查看各自说明,他们都有decode、encode方法,
decode用于将str字符串解码为unicode字符串,
encode用于将unicode字符串编码为str字符串。
在指定编码如下时有
#-*- encoding: UTF-8 -*-
示例解析:
>>> s='hello 世界' //s为str类型,utf-8编码
>>> dutf8=s.decode('gbk') //与源编码不符,用gbk解码会出错
>>> dgbk=s.decode('utf-8') //解码正确
>>> us=dgbk.encode('gbk') //编码转换正确,us为utf-8编码
总结:
1、unicode编码为中转编码,编码顺序为:
decode encode
str (utf-8编码) ------> unicode ------> str(gbk编码或者其他编码)
2、要显示汉字必须用unicode编码途径有二
1. s.decode("utf-8")
2. u"字符串"
3、由上可知,要想转码正确,首先必须知道转换对象的正确编码。
相关文档:
client:
import socket, sys
if __name__ == '__main__':
#处理参数
argv = sys.argv
if (len(argv)!=3) or (len(argv)==2 and argv[1]=='/?'):
print '>>>Useage:', argv[0], '<address> < ......
客户给一堆图片要传到后台,图片太大了,上百张图用photoshop改太慢,就想到用python写个简单的批处理。功能简单就是把原图按比例缩小,代码更简单 20多行。
# -*- coding: cp936 -*-
import Image
import glob, os
#图片批处理
def timage():
for files in glob.glob('D:\\1\\*.JPG'):
filepath,filena ......
1、升级系统
yum check-update
yum update
2、安装一些常用的工具
yum install ntp iptraf sysstat screen subversion wget bzip2 nfs-utils vim-common
3、设置每天自动核准时间
# crontab -e
0 * * * * /usr/sbin/ntpdate 210.72.145.44
:wq
4、安装一些开发包
# yum install make gcc gcc-c++ libjpeg-devel ......
#!/usr/bin/env python """
HMM module This module implements simple Hidden Markov Model class. It follows the description in
Chapter 6 of Jurafsky and Martin (2008) fairly closely, with one exception: in this
implementation, we assume that all states are initial states. @author: R ......
目前编译器(实际是翻译器)项目已经完成,对python的使用有了更深的感受。
除了之前说的以外,以下是补充几点(完全是个人看法)。
首先是python相对路径读取配置文件和写文件问题,相对路径在python中的使用跟java和C++不同。它是在那个位置运行py脚本,就把当前路径作为根路径。如:当在目录A下运行一个py脚本,那么目 ......