python常用語句

下面的語句是我工作中常用到的一些語句使用,可能比較簡單,部分參考了網上的代碼,just for note

零、參考鏈接

https://blog.csdn.net/yingshukun/article/details/53470424

https://www.runoob.com/python/python-files-io.html

一、操作excel表格

from openpyxl import Workbook
from openpyxl import load_workbook
#初始化操作
wb = load_workbook("xxx.xlsx")
#wb = openpyxl.Workbook()
#獲取所有表格的名字
sheets = wb.sheetnames
#選擇第二個表格:
sheets_first = sheets[1]
#mySheet = wb.create_sheet(index=0, title="sheet1")
#獲取特定的worksheet
ws = wb[sheets_first]
rows = ws.rows
columns = ws.columns

#獲取當前空行
def get_current_row():
    for per_row in range(1,1024):
        if ws.cell(row=per_row,column=1).value == None:
            current_row = per_row
            return current_row

#向單元格寫入內容
ws.cell(row=current_row, column=1).value = '1'
ws.cell(row=current_row, column=2).value = '2'
ws.cell(row=current_row, column=3).value = '3'
wb.save("xxx.xlsx")

#查看所有行的數據
def read_every():
    for row in rows:
        line = [col.value for col in row]
        print(line)

二、操作word中的表格

from docx import Document
document = Document("xxx.docx")
#獲取word中的表格
tables = document.tables
table = tables[0]
document.save("xxx.docx")

三、獲取文件md5

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

四、釋放zip文件

import zipfile
def freedfile():
    zip_file = zipfile.ZipFile("xxx.zip")
    zip_file.extractall('xxx')
    #如果有密碼
    zip_file.extractall('xxx',pwd="password")

五、遍歷文件夾裏面的文件

for (dirpath, dirnames, filenames) in os.walk('dir'):
    for filename in filenames:
        print(filename)

六、操作sqlite3

import sqlite3
def write_db():
    conn = sqlite3.connect('file_md5.db')
    c = conn.cursor()

    try:
        c.execute('drop table old_zip')
        c.execute('drop table new_zip')
    except:
        print('first run,init')
    c.execute('create table old_zip (name char(50) unique,md5 char(50))')
    c.execute('create table new_zip (name char(50) unique,md5 char(50))')

    for (dirpath, dirnames, filenames) in os.walk('old_zip'):
        for filename in filenames:
            path = dirpath + '/' + filename
            file_md5 = GetFileMd5(path)
            filename = (dirpath + '_' + os.path.splitext(filename)[0] + os.path.splitext(filename)[1])[8:]
            tmp = "insert or ignore into old_zip (name,md5) values('%s','%s')" % (filename, file_md5)
            c.execute(tmp)

    for (dirpath, dirnames, filenames) in os.walk('new_zip'):
        for filename in filenames:
            path = dirpath + '/' + filename
            file_md5 = GetFileMd5(path)
            filename = (dirpath + '_' + os.path.splitext(filename)[0] + os.path.splitext(filename)[1])[8:]
            tmp = "insert or ignore into new_zip (name,md5) values('%s','%s')" % (filename, file_md5)
            c.execute(tmp)

    conn.commit()
    conn.close()

七、sqlite3兩表比較

conn = sqlite3.connect('file_md5.db')
c = conn.cursor()

# same
tmp = 'select * from old_zip inner join new_zip on old_zip.name = new_zip.name and old_zip.md5 = new_zip.md5'
cursor = c.execute(tmp)
values_same = cursor.fetchall()
print(len(values_same))
# different
tmp = 'select * from old_zip inner join new_zip on old_zip.name = new_zip.name and old_zip.md5 != new_zip.md5'
cursor = c.execute(tmp)
values_different = cursor.fetchall()
print(len(values_different))
# left(old_zip)
tmp = 'select * from old_zip where not exists(select 1 from new_zip where new_zip.name = old_zip.name)'
cursor = c.execute(tmp)
values_left = cursor.fetchall()
print(len(values_left))
# right(new_zip)
tmp = 'select * from new_zip where not exists(select 1 from old_zip where new_zip.name = old_zip.name)'
cursor = c.execute(tmp)
values_right = cursor.fetchall()
print(len(values_right))

conn.commit()
conn.close()

八、執行系統命令

import subprocess
subprocess.Popen('ls -al',shell=True)

九、調用google翻譯

#pip install PyExecJS
import execjs  
import urllib.request
import sys
  
class Py4Js():  
      
    def __init__(self):  
        self.ctx = execjs.compile(""" 
        function TL(a) { 
        var k = ""; 
        var b = 406644; 
        var b1 = 3293161072; 
         
        var jd = "."; 
        var $b = "+-a^+6"; 
        var Zb = "+-3^+b+-f"; 
     
        for (var e = [], f = 0, g = 0; g < a.length; g++) { 
            var m = a.charCodeAt(g); 
            128 > m ? e[f++] = m : (2048 > m ? e[f++] = m >> 6 | 192 : (55296 == (m & 64512) && g + 1 < a.length && 56320 == (a.charCodeAt(g + 1) & 64512) ? (m = 65536 + ((m & 1023) << 10) + (a.charCodeAt(++g) & 1023), 
            e[f++] = m >> 18 | 240, 
            e[f++] = m >> 12 & 63 | 128) : e[f++] = m >> 12 | 224, 
            e[f++] = m >> 6 & 63 | 128), 
            e[f++] = m & 63 | 128) 
        } 
        a = b; 
        for (f = 0; f < e.length; f++) a += e[f], 
        a = RL(a, $b); 
        a = RL(a, Zb); 
        a ^= b1 || 0; 
        0 > a && (a = (a & 2147483647) + 2147483648); 
        a %= 1E6; 
        return a.toString() + jd + (a ^ b) 
    }; 
     
    function RL(a, b) { 
        var t = "a"; 
        var Yb = "+"; 
        for (var c = 0; c < b.length - 2; c += 3) { 
            var d = b.charAt(c + 2), 
            d = d >= t ? d.charCodeAt(0) - 87 : Number(d), 
            d = b.charAt(c + 1) == Yb ? a >>> d: a << d; 
            a = b.charAt(c) == Yb ? a + d & 4294967295 : a ^ d 
        } 
        return a 
    } 
    """)  
          
    def getTk(self,text):  
        return self.ctx.call("TL",text)

def open_url(url):
    headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:23.0) Gecko/20100101 Firefox/23.0'}
    req = urllib.request.Request(url=url, headers=headers)
    response = urllib.request.urlopen(req)
    data = response.read().decode('utf-8')
    return data

def translate(content, tk):
    global rules_lines
    if len(content) > 4891:
        print("翻譯的長度超過限制!!!")
        return

    content = urllib.parse.quote(content)

    url = "http://translate.google.cn/translate_a/single?client=t" + "&sl=en&tl=zh-CN&hl=zh-CN&dt=at&dt=bd&dt=ex&dt=ld&dt=md&dt=qca" + "&dt=rw&dt=rm&dt=ss&dt=t&ie=UTF-8&oe=UTF-8&clearbtn=1&otf=1&pc=1" + "&srcrom=0&ssel=0&tsel=0&kc=2&tk=%s&q=%s" % (
    tk, content)

    # 返回值是一個多層嵌套列表的字符串形式,解析起來還相當費勁,寫了幾個正則,發現也很不理想,
    # 後來感覺,使用正則簡直就是把簡單的事情複雜化,這裏直接切片就Ok了
    result = open_url(url)

    end = result.find("\",")
    print(result[4:end])

if __name__ == "__main__":
    js = Py4Js()
    tk = js.getTk(sys.argv[1])
    translate(sys.argv[1], tk)

十、I/O操作以及操作文件

輸入

raw_input([prompt]) 函數從標準輸入讀取一個行,並返回一個字符串(去掉結尾的換行符)

input([prompt]) 函數和 raw_input([prompt]) 函數基本類似,但是 input 可以接收一個Python表達式作爲輸入,並將運算結果返回

打開文件模式

讀寫文件可能會遇到編碼問題,可以加入encoding參數

常用的:utf-8、gbk、ansi

eg:f = open("test.txt","w+",encoding="utf-8")

十一、正則表達式

import re
#定義特徵
rule = "alert tcp any any -> any $HTTP_PORTS (msg:"xxx"l sid:12345;)"
pattern = re.compile(r'sid\s*\d{3,8}\s*;')
pattern1 = re.compile(r'\d{1,10}')
#精確搜索
sid_tmp = pattern.search(rule).group()
sid = pattern1.search(sid_tmp).group

 

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章