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、由上可知,要想转码正确,首先必须知道转换对象的正确编码。
相关文档:
Python lists have a built-in sort() method that modifies the list in-place and a sorted() built-in function that builds a new sorted list from an iterable.
There are many ways to use them to sort data and there doesn't appear to be a single, central place in the various man ......
#coding=utf-8
from newtest.wiki.models import WiKi
from django.template import loader, Context
from django.http import HttpResponse, HttpResponseRedirect
from django.shortcuts import render_to_response
def index(request, pagename=""):
"""显示正常页面,对页面的文字做特殊的链接处理"""
......
一个Python脚本的开发全过程
问题:完成一个可以为我们所有的重要程序做备份的程序。
步骤拆解:
需要备份的文件和目录由一个列表指定。
文件备份成一个zip文件。
zip存档的名称是当前的日期和时间。
我们使用标准的zip命令,它通常默认地随Linux/Unix发行版提供。Windows用户可以使用Info-Zip程序。注意 ......
Python基础(chapter3)
1 setence and syntax语句和语法
1.1 #为注释符号
1.2 \n是标准行分隔符, 通常一个语句一行
1.3 反斜线\表示下一行继续, 用来将单条语句放入多行…尽量使用括号代替
1.4 分号;表示将两个语句连接在一行中…不提倡
1.5 冒号:表示将代码块的头和体分开
1.6 语句(代码块)用缩进块方式 ......