#!/usr/bin/env python
# -*- coding: utf-8 -*-
import re
import argparse
import os
import commands
import hashlib
import subprocess
import sys
import shutil
import mysqldb
import mysqldb.cursors
reload(sys)
sys.setdefaultencoding( "utf-8" )
class log:
@staticmethod
def e(msg):
print(log.red + '[x]:' + msg + log.end)
@staticmethod
def w(msg):
print(log.yellow + '[!]:' + msg + log.end)
@staticmethod
def i(msg):
print(log.green + '[+]:' + msg + log.end)
@staticmethod
def t(msg):
print(log.blue + '[-]:' + msg + log.end)
red = '\033[1;31m'
green = '\033[1;32m'
yellow = '\033[1;33m'
blue = '\033[1;34m'
end = '\033[00m'
class get_database_info(object):
def connectdb(self):
log.w("start to connect database")
#本地連線
db = mysqldb.connect(host='ip位址', port=埠號, user='使用者', passwd='密碼', db='資料庫名稱', charset='latin1')
log.e("connect success")
return db
def select_table(self,table,db):
cursor = db.cursor()
sql='select id,name1,name2 from '+table
cursor.execute(sql)
#列印資料
log.w("select database, results list:")
results = cursor.fetchall()
name1_info=dict()
name2_info=dict()
if results:
for rec in results:
print rec[0],rec[1],rec[2]
name1_info.update()
name2_info.update()
log.w("get name1_info alias:")
for key,values in name1_info.items():
print str(key)+"="+str(values)
log.w("get name2_info alias:")
for key,values in name2_info.items():
print str(key)+"="+str(values)
cursor.close()
return name1_info,name2_info
def invokedb(self):
#連線資料庫
db = self.connectdb()
product_table="表名"
info=self.select_table(product_table,db)
#關閉資料庫
db.close()
return info
#匹配查詢到的具體值
def match(self,info,manufacturer):
marketcode=info[manufacturer]
log.w("get info of "+manufacturer+":")
print manufacturer+"="+str(marketcode)
return marketcode
def parserparameter():
parser = argparse.argumentparser(description='test!')
required = parser.add_argument_group('required arguments')
parser.add_argument('--code', required=true, help='code')
args = parser.parse_args()
return args
if __name__ == '__main__':
### 解析引數
args=parserparameter()
code=args.code
#初始化類
get_database_info = get_database_info()
name1_info,name2_info=get_database_info.invokedb()
print name1_info,name2_info
#匹配具體值
name1=get_database_info.match(name1_info,code)
name1=str(name1)
log.i("vendor="+vendor+" \nname1:"+name1)
name2=get_database_info.match(name2_info,code)
name2=str(name2)
log.i("vendor="+vendor+" \nname2:"+name2)
execl中匹配資料查詢
功能 excel中已知學生姓名找到對應 及其他資訊,或已知 查詢對應學生姓名及其他資訊。同名問題如有好的方法解決,希望能夠分享一下,因為公式只能匹配到第乙個符合的專案 示例 a列,b列為資訊表,c列為已知資訊 如 學生姓名,c列資訊在a列中查詢,找到則輸出資訊表的對應行的資訊 a列,b列 行號 a列...
有關日期引數匹配資料庫資料的方法
應用場景 從前台傳來乙個時間段 開始日期 start 如2012 11 05 結束時間 end 如2012 11 15 從資料庫查詢 從日期 start 到 日期end 之間所有符合條件的資料集合,返回乙個 list 物件例如 返回指定 觀測站某時間段內每天8時的水位集合 override publ...
linux grep多條件匹配資料
1 建立測試資料 root linuxprobe test3 cat a.txt w r t fs 4 66a g g s d g r 4d e w h s f g h 2 root linuxprobe test3 grep e w a h a.txt 提取以w開頭或者以a開頭或者以h結尾的行 a...