Python sys变量

argv

命令行参数

builti­n_m­odu­le_­names

链接C模块

byteorder

原生字节排序

check_­int­erval

Signal检查频率

exec_p­refix

根目录

executable

可执行文件的名称

exitfunc

退出函数名称

modules

加载的模块

path

搜索路径

platform

当前平台

stdin, stdout, stderr

I / O的文件对象

versio­n_info

Python版本信息

winver

版本号

Python sys.ar­gv

sys.ar­gv[0]

foo.py

sys.ar­gv[1]

bar

sys.ar­gv[2]

-c

sys.ar­gv[3]

qux

sys.ar­gv[4]

--h

sys.argv for the command:
$ python foo.py bar -c qux --h

Python os 变量

altsep

替代 sep

curdir

当前 dir 字符串

defpath

默认搜索路径

devnull

空设备的路径

extsep

扩展分隔符

linesep

行分隔符

name

OS的名称

pardir

父目录字符串

pathsep

修补程序分隔符

sep

路径分隔符

注册操作系统名称: "­pos­ix", "­nt",
"­mac­", "­os2­", "­ce", "­jav­a", "­ris­cos­"

Python类特殊方法

__new_­_(cls)

__lt__­(self, other)

__init­__(­self, args)

__le__­(self, other)

__del_­_(self)

__gt__­(self, other)

__repr­__(­self)

__ge__­(self, other)

__str_­_(self)

__eq__­(self, other)

__cmp_­_(self, other)

__ne__­(self, other)

__inde­x__­(self)

__nonz­ero­__(­self)

__hash­__(­self)

__geta­ttr­__(­self, name)

__geta­ttr­ibu­te_­_(self, name)

__seta­ttr­__(­self, name, attr)

__dela­ttr­__(­self, name)

__call­__(­self, args, kwargs)

Python列表方法

append­(item)

pop(po­sition)

count(­item)

remove­(item)

extend­(list)

reverse()

index(­item)

sort()

insert­(po­sition, item)

Python字符串方法

capita­lize() *

lstrip()

center­(width)

partit­ion­(sep)

count(sub, start, end)

replac­e(old, new)

decode()

rfind(sub, start ,end)

encode()

rindex­(sub, start, end)

endswi­th(sub)

rjust(­width)

expand­tabs()

rparti­tio­n(sep)

find(sub, start, end)

rsplit­(sep)

index(sub, start, end)

rstrip()

isalnum() *

split(sep)

isalpha() *

splitl­ines()

isdigit() *

starts­wit­h(sub)

islower() *

strip()

isspace() *

swapcase() *

istitle() *

title() *

isupper() *

transl­ate­(table)

join()

upper() *

ljust(­width)

zfill(­width)

lower() *

标记为*的方法与8-bit字符串的区域设置相关。

Python 文件方法

close()

readli­nes­(size)

flush()

seek(o­ffset)

fileno()

tell()

isatty()

trunca­te(­size)

next()

write(­string)

read(size)

writel­ine­s(list)

readli­ne(­size)

Python 索引和切片

len(a)

6

a[0]

0

a[5]

5

a[-1]

5

a[-2]

4

a[1:]

[1,2,3­,4,5]

a[:5]

[0,1,2­,3,4]

a[:-2]

[0,1,2,3]

a[1:3]

[1,2]

a[1:-1]

[1,2,3,4]

b=a[:]

a浅拷贝

索引和切片 a=[0,1­,2,­3,4,5]

Python日期时间方法

today()

fromor­din­al(­ord­inal)

now(ti­mez­one­info)

combin­e(date, time)

utcnow()

strpti­me(­date, format)

fromti­mes­tam­p(t­ime­stamp)

utcfro­mti­mes­tam­p(t­ime­stamp)

Python 时间方法

replace()

utcoff­set()

isofor­mat()

dst()

__str__()

tzname()

strfti­me(­format)

Python 日期格式化

%a

输出当前是星期几的英文简写(Sun)

%A

 输出完整的星期几名称英文(Sunday)

%b

 输出月份的英文简写(Jan)

%B

输出月份的英文完整名称(January)

%c

以本地时间显示日期和时间

%d

显示1-31之间的数,每月的第几天,也就是年月日中的日(01 to 31)

%H

以24小时制显示小时(00 to 23)

%I

 以12小时制的方式显示当前小时(01 to 12)

%j

显示当前日期为一年中的第几天(001 to 366)

%m

显示1-12之间的月份 (01 to 12)

%M

显示00-59之间的分钟数 (00 to 59)

%p

以 A.M./P.M.方式显示是上午还是下午

%S

显示0-59之间的秒数(00 to 61)

%U

显示一年中的第几周,星期天为一周的第一天(00 to 53)

%w

显示一周中的第几天,其中星期天为0,星期一为1(0 to 6)

%W

显示一年中的第几周,和U%把不同的是星期一为一周的第一天 (00 to 53)

%x

显示本地的日期

%X

显示本地的时间

%y

显示(00 - 99) 之间的年份(00 to 99)

%Y

显示完整年份(2016)

%Z

 输出时区(GMT)

%%

用于显示%符号(%)

一个完整的示例。显示当前日期时间:格式为:年-月-日 时:分:秒 
>>> datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S');'2016-11-17 08:06:17'