Python Tips

代码开发,还是需要把心沉下去。
这里记录一下今天(昨天)使用的一些代码。
(年龄大了,记性就差了,好记性不如烂笔头,记下来,以后查也方便些)

判断数组里是否存在指定元素

# coding=utf-8
item=5
var_array = [1,2,3,4,6]
if item in var_array:
    print("%d 在数组中" % item)
else:
    print("%d 不在数组中" % item)
# 将输出
# 5 不在数组中

判断指定的键是否在字典中存在

# coding=utf-8
key = "god"
var_dict = {"god":1, "human":2, "animal":3}
if key in var_dict:
    print("%s 在字典中存在"%key)
else:
    print("%s 在字典中不存在"%key)
# 将输出
# god 在字典中存在

合并两个数组(取交集)

arr1 = [1,2,3,5]
arr2 = [4,5,8,9]
new_array = list( set(arr1) & set(arr2) )
print(new_array)
# 将输出
# [5]

合并两个数组(取并集)

方法一

arr1 = [1,2,3,5]
arr2 = [4,5,8,9]
new_array = list( set(arr1) | set(arr2) )
print(new_array)
# 将输出
# [1, 2, 3, 4, 5, 8, 9]

方法二

arr1 = [1,2,3,5]
arr2 = [4,5,8,9]
# 以并集方式合并
arr1.extend(arr2)
# 去重
new_array = list(set(arr1))
print(new_array)
# 将输出
# [1, 2, 3, 4, 5, 8, 9]

合并两个数组(取差集/取补集)

arr1 = ["a","b","c","d"]
arr2 = ["d","e"]
new_array = list(set(arr2)-set(arr1))
print(new_array)
# 将输出
# ["e"]

通过 requests 发送带自定义 headers 的 post 请求

# coding=utf-8
import requests
api = 'http://httpbin.org/post?t=333'
headers = {'TOKEN':'testavbs'}
form = {"a":123,"b":456}
try:
    response = requests.post(api, headers=headers, data=form)
except requests.RequestException as e:
    print(dict(error=e.message))
print(response.text)
print("Status code: {}").format(response.status_code)
# 将输出
# {
#   "args": {
#     "t": "333"
#   }, 
#   "data": "", 
#   "files": {}, 
#   "form": {
#     "a": "123", 
#     "b": "456"
#   }, 
#   "headers": {
#     "Accept": "*/*", 
#     "Accept-Encoding": "gzip, deflate", 
#     "Content-Length": "11", 
#     "Content-Type": "application/x-www-form-urlencoded", 
#     "Host": "httpbin.org", 
#     "Token": "testavbs", 
#     "User-Agent": "python-requests/2.21.0"
#   }, 
#   "json": null, 
#   "url": "https://httpbin.org/post"
# }
# Status code: 200

快速检测指定文件的 MIME 类型

这里通过使用一个第三方包 python-magic来进行快速检测

在 OSX 下使用,请注意,python-magic 依赖 libmagic 这个类库,需要先安装

安装

brew install libmagic
pip install python-magic

实例

import magic
mime = magic.Magic(mime=True)
result = mime.from_file("test/test.png")
print(result)
# 将输出
# 'image/png'

通过 mimetypes 库检测

from mimetypes import MimeTypes
import urllib 
mime = MimeTypes()
url = 'https://www.dogedoge.com/assets/logo_homepage.normal.v108.svg'
url = urllib.pathname2url(url)
mime_type = mime.guess_type(url)
print(mime_type)
# 将输出
# ('image/svg+xml', None)

计算指定文件的 md5 值

import hashlib
import os

filepath = 'test.png'

def get_file_md5(filepath):
    if not os.path.isfile(filepath):
        return
    myhash = hashlib.md5()
    f = open(filepath,'rb')
    while True:
        b = f.read(8096)
        if not b :
            break
        myhash.update(b)
    f.close()
    return myhash.hexdigest()

result = get_file_md5(filepath)

遍历指定目录及子目录

# coding=utf-8
import os

# 递归遍历目录
def traversal_files(path):
    for filepath in os.listdir(path):
        filepath = os.path.join(path, filepath)
        if os.path.isfile(filepath):
            # 这里可以写针对文件的操作逻辑
			print(filepath)
        # 判断当前目录是否为文件夹
        if os.path.isdir(filepath):
            # print('即将遍历: %s' % filepath)
            traversal_files(filepath)

scan_dir = '/tmp'
traversal_files(scan_dir)

参考

赞赏