标题 创建时间 发布时间 最后一次修改 备注
Python 常见问题速查 2020.08.04 2020.12.21 2021.03.30 /

获取日期参数输入,或者默认日期

import sys
import time

if len(sys.argv)==2:
    today = sys.argv[1]
else:
    today = time.strftime("%Y-%m-%d", time.localtime())

日期加减操作

from datetime import datetime, timedelta

def date_transfer(today):
  today = datetime.strptime(today, "%Y-%m-%d")
  yesterday = today - timedelta(days=1)

  return today.strftime( "%Y-%m-%d"),yesterday.strftime( "%Y-%m-%d")

对list元素进行判断

# source: https://thispointer.com/python-count-elements-in-a-list-that-satisfy-certain-conditions/

listOfElems = [11, 22, 33, 45, 66, 77, 88, 99, 101]
# count numbers in the list which are greater than 5
count = sum(map(lambda x : x>5, listOfElems)) # lambda 函数可替换

print('Count of numbers in a list which are greater than 5: ', count)

str类型的array转array

def str2array(current_str):
    '''
    返回数值数组
    '''
    current_array = current_str.replace(']','').replace('[','').replace('"','').split(",")
    current_array = np.array(current_array)
    return [int(numeric_string) for numeric_string in current_array]
  
current_str = '["1","2"]'
str2array(current_str)
>>> [1, 2]

python 取整

# refer: http://kuanghy.github.io/2016/09/07/python-trunc

import math
# 向上取整
math.ceil(2.25)
>>> 3

# 向下取整
int(3.4)
>>> 3
math.floor(2.75)
>>> 2

# 特别注意⚠️ round 取整
round(3.5)
>>> 4
round(2.5)
>>> 2

# 为何会这样:是为了保证整体分布和均值不变。如果每遇*.5进一,则整体均值会上移,带来误差。

HMAC SHA256 加密

# refer: https://www.gauravvjn.com/generate-hmac-sha256-signature-in-python/
import hmac
import hashlib 
import binascii

def create_sha256_signature(key, message):
    byte_key = binascii.unhexlify(key)
    message = message.encode()
    return hmac.new(byte_key, message, hashlib.sha256).hexdigest().upper()

create_sha256_signature("E49756B4C8FAB4E48222A3E7F3B97CC3", "TEST STRING")

str类型的dict转dict

# refer: https://stackoverflow.com/questions/988228/convert-a-string-representation-of-a-dictionary-to-a-dictionary
s = "{'muffin' : 'lolz', 'foo' : 'kitty'}"

import ast
ast.literal_eval(s)

>>> {'muffin': 'lolz', 'foo': 'kitty'}

list 两两配对 pairwise

# refer: https://stackoverflow.com/questions/40092474/get-all-pairwise-combinations-from-a-list

import itertools
x = [1,2,3,4]
list(itertools.combinations(x, 2))

>>> [(1, 2), (1, 3), (1, 4), (2, 3), (2, 4), (3, 4)]

list count item

# https://stackoverflow.com/questions/2600191/how-can-i-count-the-occurrences-of-a-list-item

l = ["a","b","b"]
from collections import Counter
Counter(l)
>>> Counter({'b': 2, 'a': 1})

读取 txt 文本文件

# https://stackoverflow.com/questions/8369219/how-to-read-a-text-file-into-a-string-variable-and-strip-newlines

with open('data.txt', 'r') as file:
    data = file.read().replace('\n', '')

每 n 个距离截断 string

# https://stackoverflow.com/questions/9475241/split-string-every-nth-character

line = '1234567890'
n = 2
[line[i:i+n] for i in range(0, len(line), n)]
>>> ['12', '34', '56', '78', '90']

OptionParser 获取命令行参数

# http://www.ttlsa.com/python/python-optionparser-usage/
# https://docs.python.org/3/library/optparse.html
from optparse import OptionParser
...
parser = OptionParser()
parser.add_option("-f", "--file", dest="filename",
                  help="write report to FILE", metavar="FILE")
parser.add_option("-q", "--quiet",
                  action="store_false", dest="verbose", default=True,
                  help="don't print status messages to stdout")

(options, args) = parser.parse_args()

关于作者