...
Python sys Variables
变量 |
说明 |
argv |
Command line args |
builtin_module_names |
Linked C modules |
byteorder |
Native byte order |
exec_prefix |
Root directory |
executable |
Name of executable |
modules |
Loaded modules |
path |
Search path |
platform |
Current platform |
stdin, stdout, stderr |
File objects for I/O |
version_info |
Python version info |
version |
Version number |
Python sys.argv
sys.argv for the command:
$ python foo.py bar -c qux --h
变量 |
值 |
sys.argv[0] |
foo.py |
sys.argv[1] |
bar |
sys.argv[2] |
-c |
sys.argv[3] |
qux |
sys.argv[4] |
–h |
Python os Variables
方法 |
说明 |
altsep |
Alternative sep |
curdir |
Current dir string |
defpath |
Default search path |
devnull |
Path of null device |
extsep |
Extension separator |
linesep |
Line separator |
name |
Name of OS |
pardir |
Parent dir string |
pathsep |
Patch separator |
sep |
Path separator |
Registered OS names: “posix”, “nt”, “mac”, “os2”, “ce”, “java”, “riscos”
Python Class Special Methods
方法 |
说明 |
_new_(cls) |
|
init(self, args) |
|
_del_(self) |
|
_str_(self) |
|
repr(self) |
|
lt(self, other) |
|
le(self, other) |
|
gt(self, other) |
|
ge(self, other) |
|
eq(self, other) |
|
ne(self, other) |
|
_cmp_(self, other) |
|
index(self) |
|
nonzero(self) |
|
hash(self) |
|
getattr(self, name) |
|
_getattribute_(self, name) |
|
setattr(self, name, attr) |
|
delattr(self, name) |
|
call(self, args, kwargs) |
|
Python List Methods
方法 |
说明 |
append(item) |
|
pop(position) |
|
count(item) |
|
remove(item) |
|
extend(list) |
|
reverse() |
|
index(item) |
|
sort() |
|
insert(position, item) |
|
Python String Methods
方法 |
说明 |
decode() |
|
encode() |
|
count(sub, start, end) |
|
index(sub, start, end) |
|
rindex(sub, start, end) |
|
find(sub, start, end) |
|
rfind(sub, start ,end) |
|
startswith(sub) |
|
endswith(sub) |
|
center(width) |
|
rjust(width) |
|
ljust(width) |
|
zfill(width) |
|
expandtabs() |
|
strip() |
|
lstrip() |
|
rstrip() |
|
split(sep) |
|
rsplit(sep) |
|
splitlines() |
|
partition(sep) |
|
rpartition(sep) |
|
join() |
|
swapcase() * |
|
capitalize() * |
|
title() * |
|
translate(table) |
|
lower() * |
|
upper() * |
|
replace(old, new) |
|
isdigit() * |
|
isalnum() * |
|
isspace() * |
|
istitle() * |
|
islower() * |
|
isupper() * |
|
isalpha() * |
|
Methods marked * are locale dependant for 8-bit strings.
Python File Methods
方法 |
说明 |
close() |
|
readlines(size) |
|
flush() |
|
seek(offset) |
|
fileno() |
|
tell() |
|
isatty() |
|
truncate(size) |
|
next() |
|
write(string) |
|
read(size) |
|
writelines(list) |
|
readline(size) |
|
Python Indexes and Slices
Indexes and Slices of a=[0,1,2,3,4,5]
操作 |
结果 |
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[:] |
Shallow copy of a |
Python Datetime Methods
操作 |
结果 |
today() |
|
fromordinal(ordinal) |
|
now(timezoneinfo) |
|
combine(date, time) |
|
utcnow() |
|
strptime(date, format) |
|
fromtimestamp(timestamp) |
|
utcfromtimestamp(timestamp) |
|
Python Time Methods
操作 |
结果 |
replace() |
|
utcoffset() |
|
isoformat() |
|
dst() |
|
str() |
|
tzname() |
|
strftime(format) |
|
Python Date Formatting
字符 |
说明 |
%a |
Abbreviated weekday (Sun) |
%A |
Weekday (Sunday) |
%b |
Abbreviated month name (Jan) |
%B |
Month name (January) |
%c |
Date and time |
%d |
Day (leading zeros) (01 to 31) |
%H |
24 hour (leading zeros) (00 to 23) |
%I |
12 hour (leading zeros) (01 to 12) |
%j |
Day of year (001 to 366) |
%m |
Month (01 to 12) |
%M |
Minute (00 to 59) |
%p |
AM or PM |
%S |
Second (00 to 61⁴) |
%U |
Week number¹ (00 to 53) |
%w |
Weekday² (0 to 6) |
%W |
Week number³ (00 to 53) |
%x |
Date |
%X |
Time |
%y |
Year without century (00 to 99) |
%Y |
Year (2008) |
%Z |
Time zone (GMT) |
%% |
A literal “%” character (%) |
¹ Sunday as start of week. All days in a new year preceding the first Sunday are considered to be in week 0.
² 0 is Sunday, 6 is Saturday.
³ Monday as start of week. All days in a new year preceding the first Monday are considered to be in week 0.
⁴ This is not a mistake. Range takes account of leap and double-leap seconds.
pdf下载
参考
- 原文 - Python Cheat Sheet by DaveChild
- Python 速查表