作为一个Python菜鸡,有些常用的代码和方法还是要整理一下的:
#!python3 ################################################################################################################################################################ # 多行代码可以使用反斜线,一种经典的做法 ################################################################################################################################################################ ################################################################################################################################################################ # 打印字典或者数组什么的 # https://stackoverflow.com/questions/15785719/how-to-print-a-dictionary-line-by-line-in-python ################################################################################################################################################################ print(json.dumps(dicObject, indent = 4, ensure_ascii=False)) # 并显示UNICODE字符,而不时是转义序列 ################################################################################################################################################################ # Python String endswith() Method # https://www.tutorialspoint.com/python/string_endswith ################################################################################################################################################################ str.startswith(suffix[, start[, end]]) str.endswith(suffix[, start[, end]]) ################################################################################################################################################################ # TypeError: '<' not supported between instances of 'dict' and 'dict' # https://stackoverflow.com/questions/55695479/typeerror-not-supported-between-instances-of-dict-and-dict ################################################################################################################################################################ dic.sort(key=lambda x: x[0]['name'], reverse=False) ################################################################################################################################################################ # Python3 格式化输出 %s & %d 等 / python - heredoc or multiline string # https://www.cnblogs.com/alfred0311/p/7735539.html # http://lofic.github.io/tips/python-heredoc.html ################################################################################################################################################################ print ("Name:%-10s Age:%08d Height:%08.2f" % ("Alfred", 25, 1.70)) "{name} was {place}".format(name='Louis', place='here') ################################################################################################################################################################ # 如何在 Python3 中定义和使用常量 # https://blog.csdn.net/jieming2002/article/details/78379264 TODO 需要定义一个类 ################################################################################################################################################################ ################################################################################################################################################################ # Python here document without newlines at top and bottom # https://stackoverflow.com/questions/9589301/python-here-document-without-newlines-at-top-and-bottom ################################################################################################################################################################ print ''' dog cat '''[1:-1] print ''' dog cat '''.strip() ################################################################################ # How to get the date N days ago in Python # https://www.saltycrane.com/blog/2010/10/how-get-date-n-days-ago-python/ ################################################################################ from datetime import datetime, timedelta date_N_days_ago = datetime.now() - timedelta(days=N) ################################################################################################################################################################ # Python之三目运算符 # https://www.cnblogs.com/wanghao123/p/7921654.html ################################################################################################################################################################ a = x if (x>y) else y ################################################################################################################################################################ # What's the correct way to convert bytes to a hex string in Python 3? # https://stackoverflow.com/questions/6624453/whats-the-correct-way-to-convert-bytes-to-a-hex-string-in-python-3 ################################################################################################################################################################ b'\xde\xad\xbe\xef'.hex() bytes.fromhex('deadbeef') ################################################################################################################################################################ # Iterate over a dictionary in Python # https://www.geeksforgeeks.org/iterate-over-a-dictionary-in-python/ ################################################################################################################################################################ for state in statesAndCapitals: print(state) ################################################################################################################################################################ # Check if a given key already exists in a dictionary # https://stackoverflow.com/questions/1602934/check-if-a-given-key-already-exists-in-a-dictionary ################################################################################################################################################################ # in is the intended way to test for the existence of a key in a dict. ################################################################################################################################################################ # Accessing the index in 'for' loops? # https://stackoverflow.com/questions/522563/accessing-the-index-in-for-loops ################################################################################################################################################################ for idx, val in enumerate(ints): print(idx, val)