Python sys Variables
变量说明argvCommand line argsbuiltin_module_namesLinked C modulesbyteorderNative byte orderexec_prefixRoot directoryexecutableName of executablemodulesLoaded modulespathSearch pathplatformCurrent platformstdin, stdout, stderrFile objects for I/Oversion_infoPython version infoversionVersion number
Python sys.argv
sys.argv for the command:
$ python foo.py bar -c qux --h
变量值sys.argv[0]foo.pysys.argv[1]barsys.argv[2]-csys.argv[3]quxsys.argv[4]–h
Python os Variables
方法说明altsepAlternative sepcurdirCurrent dir stringdefpathDefault search pathdevnullPath of null deviceextsepExtension separatorlinesepLine separatornameName of OSpardirParent dir stringpathsepPatch separatorsepPath 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 SlicesIndexes and Slices of a=[0,1,2,3,4,5]
操作结果len(a)6a[0]0a[5]5a[-1]5a[-2]4a[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
字符说明%aAbbreviated weekday (Sun)%AWeekday (Sunday)%bAbbreviated month name (Jan)%BMonth name (January)%cDate and time%dDay (leading zeros) (01 to 31)%H24 hour (leading zeros) (00 to 23)%I12 hour (leading zeros) (01 to 12)%jDay of year (001 to 366)%mMonth (01 to 12)%MMinute (00 to 59)%pAM or PM%SSecond (00 to 61⁴)%UWeek number¹ (00 to 53)%wWeekday² (0 to 6)%WWeek number³ (00 to 53)%xDate%XTime%yYear without century (00 to 99)%YYear (2008)%ZTime 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 速查表