Author: 楚格
2018-11-14 21:11:04
IDE: Pycharm2018.02 Python 3.7
KeyWord : 面向對象實例
Explain:
1.path讀取路徑
2.API獲取數據以及處理
3.思路
3.1先獲取路徑,包括動態獲取當前路徑,可視化選擇文件,簡單防止誤操作判斷。
3.2先獲取Excel中數據,固定行列位置,讀取的數據存儲在列表中,列表中的元素以字典方式存儲,便於查找。
3.3 讀取standard和actual數據,以actual的一列中的某個數據與standard的一列數據進行對比,判斷。
3.4若對比結束,返回信息。包括屏幕打印和日志保存的統計信息。
3.5正則表達,判斷首字母是新增的
3.6此部分程序可以作為模塊接口示例程序。
-----------------------------------------------------------------------------
程序結構圖

--------------------------------------------------------------------------------
path
-------
1 # coding=utf-8 2 #--------------------------------- 3 ''' 4 # Author : chu ge 5 # Function: 提取文件路徑 6 # 7 ''' 8 #--------------------------------- 9 __all__ = ['Class_File_Read'] 10 11 import os 12 13 ''' 14 # ============================================================================ 15 # Class : 類 file path 16 # Explain : 輸入參數 文件的序號 17 # : 輸出參數 global_var_file_name_file <路徑> 18 # ============================================================================ 19 ''' 20 class Class_File_Read(object): 21 def __init__(self): 22 # print('Class file read excel') 23 self.Methoons_File_Directory() 24 25 def Methods_File_Operation_Standard(self): 26 print('\nstandard input:0 ') 27 STANDARD = "0" 28 File_Path = self.Methods_Select_File(STANDARD) 29 print("Methods_File_Operation_Standard \n") 30 return File_Path 31 32 def Methods_File_Operation_Actual(self): 33 number = input("Actual Input :") 34 # number = "1" # 臨時 35 File_Path = self.Methods_Select_File(number) 36 print("Methods_File_Operation_Actual \n") 37 return File_Path 38 39 def Methoons_File_Directory(self): 40 # 初始化列表 存放文件 41 local_var_num = [] 42 local_var_file_name = [] 43 # 獲取當前文件路徑 調試使用 44 # local_var_cwd = os.getcwd() 45 # print('當前文件夾的路徑 :\n->>> < %s >' % local_var_cwd ) 46 # [dir_name, file_name] = os.path.split(local_var_cwd) 47 # print('分離files & path:< %s > < %s >'%(dir_name,file_name)) 48 # 固定文件夾目錄 49 try: 50 local_var_cwd = 'D:\Auto_Testing_Pycharm\Test_Instance\Test_User_Excel\excel' 51 local_var_list_name = os.listdir(local_var_cwd) # # print('當前文件夾下的所有文件和文件夾:') 52 except Exception: 53 print("捕獲異常:\n目錄:<%s>\n位置:<%s>"%(os.getcwd(),"固定文件夾目錄")) 54 55 56 # 遍歷文件夾 編號 + 文件名 57 for temp_var_num in range(len(local_var_list_name)): 58 # print('num:%s name:< %s >'% (temp_var_num,local_var_list_name[temp_var_num])) # display All 59 [new_file_name, file_name_end] = os.path.splitext(local_var_list_name[temp_var_num]) # file name handle 60 # print('分離文件名與后綴:< %s > < %s > '%(new_file_name,file_name_end)) # display result 61 62 # 過濾其他文件 保留Excel文件 以供選擇 63 if file_name_end == '.xlsx': 64 local_var_num.append(temp_var_num) # 存儲文件編號 65 local_var_file_name.append(local_var_list_name[temp_var_num]) # 存儲文件名稱 66 # print('Display select files : ', local_var_num, local_var_file_name) # 顯示二個列表 67 # ------------------------------------------------------------------------------------------ 68 # 顯示留存的文件 二個列表映射成字典 保留列表方便調試使用 技巧 *** 69 local_var_storage = dict(zip(local_var_num, local_var_file_name)) 70 71 # 提示 遍歷字典元素 顯示內容 local_var_storage.items() 無序 72 # 可以這個替換 : print('Your selecr pprotocol : < %s > '% local_var_storage.keys()) 73 # print('Protocols that can be executed : ') 74 for temp_var_num in local_var_storage: 75 print("", temp_var_num, local_var_storage[temp_var_num]) 76 # ----------------------------------------------------------------------------- 77 78 def Methods_Select_File(self,number): 79 global global_var_file_name_file 80 local_var_select_number = number 81 82 # 固定文件夾目錄 83 local_var_cwd = 'D:\Auto_Testing_Pycharm\Test_Instance\Test_User_Excel\excel' 84 local_var_list_name = os.listdir(local_var_cwd) # # print('當前文件夾下的所有文件和文件夾:') 85 local_var_num = [] 86 local_var_file_name = [] 87 88 for temp_var_num in range(len(local_var_list_name)): 89 [new_file_name, file_name_end] = os.path.splitext(local_var_list_name[temp_var_num]) # file name handle 90 if file_name_end == '.xlsx': 91 local_var_num.append(temp_var_num) # 存儲文件編號 92 local_var_file_name.append(local_var_list_name[temp_var_num]) # 存儲文件名稱 93 94 protocol_num = True 95 96 while protocol_num: 97 if local_var_select_number < str(len(local_var_num)): 98 print('Your select protocol : < %s > '% local_var_file_name[int(local_var_select_number)]) 99 # 拼接路徑 100 temp_var_path_string = local_var_cwd +'\\'+ local_var_file_name[int(local_var_select_number)] 101 # print('temp_var_path_string: %s '% temp_var_path_string) 102 protocol_num = False 103 global_var_file_name_file = temp_var_path_string 104 105 else: 106 print('請選擇可以執行的協議!') 107 protocol_num = True 108 109 return global_var_file_name_file # 出口 110 111 # 112 113 # ============================================================================ 114 115 ''' 116 # ============================================================================ 117 # 測試專用 118 # ============================================================================ 119 ''' 120 if __name__ == "__main__": 121 print('測試開始') 122 ''' 123 # 讀取當前文件下,Excel文件 124 ''' 125 aa = Class_File_Read() 126 # asd = aa.Methods_File_Operation_Standard() 127 # aa.Methods_File_Operation_Actual() 128 asd = aa.Methods_Select_File("1") 129 print('Path: <%s> '%(asd)) 130 print('測試完成')
-------
-----------------------------------------------------------
API
-------
# coding=utf-8 # --------------------------------- ''' # Author : chu ge # Function: # ''' # --------------------------------- ''' # ------------------------------------------ # 導入模塊 # 1.系統庫 # 2.第三方庫 # 3.相關定義庫 # ------------------------------------------ ''' # 1.系統庫 import sys import os import re # 2.第三方庫 # 讀取 Excel 模塊 import xlrd import xlwt import logging ''' # ------------------------------------------ # 導入:同級不同文件夾 # 先文件夾__init__注釋,其次引用name.py # 最后 使用具體 類class 或者 函數function 名稱 # ------------------------------------------ ''' import Test_Log ''' # ------------------------------------------ # 導入:同級同文件夾 # 先引用name.py # 后 使用具體 類class 或者 函數function 名稱 # ------------------------------------------ ''' import Testing_Read_Path # ''' # ============================================================================ # Class : 類 # Explain : 輸入參數 # : 輸出參數 # ============================================================================ ''' class Class_Module_Read(object): def __init__(self): print() def Methods_Excel_Data(self, file_path, colnameindex=0, by_name=u'Sheet1'): # 修改自己路徑 # print('Function_Excel_Table_Byname') # --------------------------------------------------------------------------------------- self.File = file_path # 路徑名稱變換 # 路徑 # print("路徑",self.File) # --------------------------------------------------------------------------------------- local_list_line = [] # Methods_Excel_Data try: # -------------------------------------------------------------------------------------- local_var_temp_data = xlrd.open_workbook(self.File) # 打開電子文檔 目的:響應刪除函數 # # 這里可以拓展,使用一個Excel 利用不同表格,可以減少外部表格的數量,與上述打開表格方式 是不同的 local_var_temp_table = local_var_temp_data.sheet_by_name(by_name) # 獲得表格 Excel中不同表格 # # Contains the data for one worksheet local_var_total_rows = local_var_temp_table.nrows # 屬性 拿到總共行數 # print('total_rows: < %d >' % local_var_total_rows) # display # # Returns a slice of the values of the cells in the given line local_var_column_names = local_var_temp_table.row_values(colnameindex, 0, 6) # 某一列數據 ['', '', '', ''] 默認從0行開始 # print('line num: < %s > names: < % s >'% (len(local_var_column_names),local_var_column_names)) for temp_num in range(1, local_var_total_rows): # 也就是從Excel第二行開始,第一行表頭不算 local_var_row = local_var_temp_table.row_values(temp_num) # 返回單元格中切片 # print('rows_num: < %s > line_num: < %s > each_element' % (temp_num ,len(local_var_row)),local_var_row ) # 每row 就是列表 # 每行分片成列表, 列表轉變成字典, 字典轉變成列表 local_dictionary_app = dict(zip(local_var_column_names, local_var_row)) # 列表變字典 # print('local_dictionary_app: ',local_dictionary_app) local_list_line.append(local_dictionary_app) # 字典轉變成列表 # # 內部的字典變成了元素 # print('local_list_line: ',local_list_line) #打印列表元素 return local_list_line except Exception: print("捕獲異常:\n位置:<%s>\n目錄:<%s>" % ("Methods_Excel_Data", os.getcwd())) def Methods_Excel_Data_Display(self, file_path): local_list_line = self.Methods_Excel_Data(file_path) # 遍歷列表中的元素 顯示成表格模式 for line in local_list_line: print('行號編碼:< %s > %s' % (local_list_line.index(line), line)) # pprint.pprint(line) # 規則打印數據 # 返回列表 並且列表的元素是字典 # ''' # ============================================================================ # Class : 類 # Explain : 輸入參數 表格數據 # : 輸出參數 分表格 # ============================================================================ ''' class Class_Module_Tlabe(object): def __init__(self): File_Path = Testing_Read_Path.Class_File_Read() # 路徑對象 path self.local_var_path_standard = File_Path.Methods_File_Operation_Standard() self.local_var_path_actual = File_Path.Methods_File_Operation_Actual() self.Log_Display = Test_Log.Test_Save_log.Class_Log_Display() self.Log_Display.Methods_BasicConfig() def Methods_Table_Standard(self): # Methods_Table_Standard try: File_Read = Class_Module_Read() # Excel data 對象 Table_Standard = File_Read.Methods_Excel_Data(self.local_var_path_standard) return Table_Standard except Exception: print("捕獲異常:\n位置:<%s>\n目錄:<%s>" % ("Methods_Table_Standard", os.getcwd())) def Methods_Table_Actual(self): # Methods_Table_Actual try: File_Read = Class_Module_Read() # Excel data 對象 Table_Actual = File_Read.Methods_Excel_Data(self.local_var_path_actual) return Table_Actual except Exception: print("捕獲異常:\n位置:<%s>\n目錄:<%s>" % ("Methods_Table_Actual", os.getcwd())) # # 搜索actual 中循環搜索的詞條,逐個與全部的標准庫進行對比 def Methods_Search_Actual_To_Standard(self): Table_Actual = self.Methods_Table_Actual() local_error_line_list = [] Feature = self.Methods_Feature_Select() # Actual_To_Standard try: local_var_order_count = 0 # 初始化計數 print("Total Actual Line : < %d >" % len(Table_Actual)) for Var_A_Element in Table_Actual: local_var_order_count += 1 # 這里是對比的源頭 # 添加規則正則表達,首字母 try: Is_Capital =self.Methods_Capital_Word(Var_A_Element[1]) if Is_Capital == True: A1 = self.Methods_Search_Standard(1, Var_A_Element[1]) # 第1列 備注 第0列開始 else: A1 = None except Exception: print("123") A2 = self.Methods_Search_Standard(2, Var_A_Element[2]) # 第2列 # 功能選擇處 if Feature == "1": A3 = "1" else: A3 = self.Methods_Search_Standard(3, Var_A_Element[3]) # 第3列 print('*' * 20) print('line Number : <%d>' % (local_var_order_count + 1)) logging.info('\nline Number : [%d]\n%s' % ((local_var_order_count + 1),"one sql end")) if ((A1 == "1") and (A2 == "1") and(A3 == "1")) != 0: print('本詞條測試結束') print("--" * 20) else: print('詞條有錯誤!!! 請查找原因 !!!') print("--" * 20) local_error_line_list.append(local_var_order_count) # test message total print("Test Message Total") for var_line in local_error_line_list: print("Error line: %s" % (var_line + 1)) # 對應Excel logging.info("Error line: %s" % (var_line + 1)) print("Total Num : <%s>" % len(local_error_line_list)) logging.info("Total Num : <%s>" % len(local_error_line_list)) except Exception: print("捕獲異常:\n位置:<%s>\n目錄:<%s>" % ("Actual_To_Standard", os.getcwd())) def Methods_Search_Standard(self, line, entry): Table_Standard = self.Methods_Table_Standard() self.loacl_var_line = line self.local_var_entry = entry Local_var_count = 1 # Search_Actual_To_Standard try: # 單個思路:獲取到對值進行匹配,此值作為對比的標點,標准庫作為標靶 # 搜索匹配功能,遍歷每行,對應元素進行匹配,這是關鍵 # 最后返回提示量 for Var_A_Element in Table_Standard: # 有詞條且詞條不為空 if (self.local_var_entry == Var_A_Element[self.loacl_var_line]) != 0: Local_Test_Flag = "1" return Local_Test_Flag else: Local_Test_Flag = "0" Local_var_count += 1 # 調試時,打開打印 print("Error name:<%s>" % self.local_var_entry) logging.info("\nError column: <%d>\n詞條名稱 : <%s>" %(self.loacl_var_line,self.local_var_entry) ) except Exception: print("捕獲異常:\n位置:<%s>\n目錄:<%s>" % ("Search_Actual_To_Standard", os.getcwd())) # 功能選擇函數 def Methods_Feature_Select(self): # 1.feature 1和2列對比(默認) # 2.feature 1、2和3列對比 print("Feature Select:\n[%s]\n[%s]"%("1.feature 1和2列對比(默認)","2.feature 1、2和3列對比")) # Feature_Name = input(">>: ") Feature_Name = "1" #調試固定 if Feature_Name >"3" : Feature_Name = "1" return Feature_Name #首字母大寫特地檢查 def Methods_Capital_Word(self,column1): result_value = re.match(r"[A-Z]",column1) if result_value: result = True # result = result2.istitle() else: result = False # print("首字母大寫特地檢查:",result) return result # ''' # ============================================================================ # Function: Function_Excel_Data 函數 # Explain : 輸入參數 對象 列表的字典 # : 輸出參數 被調用 # ============================================================================ ''' def Function_Excel_Data(): # Function_Excel_Data try: File_Path = Testing_Read_Path.Class_File_Read() # 路徑對象 path local_var_path_standard = File_Path.Methods_File_Operation_Standard() local_var_path_actual = File_Path.Methods_File_Operation_Actual() # File_Read = Class_Module_Read() # Excel data 對象 # Table_Standard = File_Read.Methods_Excel_Data(local_var_path_standard) # Table_Actual = File_Read.Methods_Excel_Data(local_var_path_actual) File_Read.Methods_Excel_Data_Display(local_var_path_standard) File_Read.Methods_Excel_Data_Display(local_var_path_actual) except Exception: print("捕獲異常:\n目錄:<%s>\n位置:<%s>" % ("Function_Excel_Data", os.getcwd())) # # ============================================================================ ''' # ============================================================================ # 測試專用 # ============================================================================ ''' if __name__ == "__main__": print('測試開始') # ============================ Table_data = Class_Module_Tlabe() Table_data.Methods_Search_Actual_To_Standard() print('測試完成')
--
------------------------------------------------
結束
