1、檔案Auto_update_data,需要處理的映射基礎資料
#定義需要匯入的映射資料 #以字典進行定義 [公司類目id : 平臺類目id] #d = {key1 : value1, key2 : value2, key3 : value3 } ap_category_relation_data =https://www.cnblogs.com/camg/archive/2022/07/19/ { 5807 : 10002200, 6375 : 10100737, 6772 : 10100733, 5816 : 1540, 5832 : 1540, }
2、檔案Auto_update_db,自己封裝的資料庫操作
#資料庫操作 import pymysql #資料庫類 class LazadaDb: def __init__(self, host, user, password, database, port): #打開資料庫連接 db = pymysql.connect( host = host, user = user, password = password, database = database, port = port ) #使用 cursor()方法創建一個游標物件 cursor self.db = db self.cursor = db.cursor() #定義一個查詢的方法 查一列 def get(self, sql): try: cursor = self.cursor #執行SQL陳述句 cursor.execute(sql) #使用 fetchone() 方法獲取單條資料 data =https://www.cnblogs.com/camg/archive/2022/07/19/ cursor.fetchone() #回傳資料 return data except: return '獲取資料出錯101' def __del__(self): #解構式 關閉資料庫連接 self.db.close()
3、檔案Auto_update_way,遞回,獲取類目樹方法
#書寫公共方法 import Auto_update_db #無限極往上獲取平臺類目樹資訊 def platformCategoryVerify(platform_category_id): tree = [] #獲取平臺資料庫類 LazadaDb = Auto_update_db.LazadaDb('資料庫ip', 'test', '密碼', 'nt_auto_publish', 3311) #拼接查詢sql sql = "SELECT category_id,category_name,parent_id,level FROM ap_categories WHERE platform = 1 AND site_code = 'MY' AND category_id = " + str(platform_category_id) #獲取類目資訊 apCategories_info = LazadaDb.get(sql) #判斷類目是否存在 if apCategories_info: #存在,通過父類id繼續獲取上級 tree = platformCategoryVerify(apCategories_info[2]) #將獲取到類目,添加到 定義的串列 tree 中 tree.append(apCategories_info) return tree #無限極往上獲取公司類目樹資訊 def companyCategoryVerify(company_category_id): tree = [] #獲取公司資料庫 SysDb = Auto_update_db.LazadaDb('資料庫ip', 'test', '密碼', 'nt_product', 3307) #拼接查詢sql sql = "SELECT id, category_name, parent_id, level FROM nt_categories WHERE status = 1 AND ID = " + str(company_category_id) #獲取類目資訊 sysCategories_info = SysDb.get(sql) #p判斷類目是否存在 if sysCategories_info: #存在,通過父類id繼續獲取上級 tree = companyCategoryVerify(sysCategories_info[2]) #將獲取到類目,添加到 定義的串列 tree 中 tree.append(sysCategories_info) return tree
4、檔案Auto_update_attr,最后組裝資料
#刷自動化類目 import Auto_update_data import Auto_update_way #獲取需要處理的類目映射資料 list_data =https://www.cnblogs.com/camg/archive/2022/07/19/ Auto_update_data.ap_category_relation_data #定義一個最終資料的串列 ap_category_relation_data =https://www.cnblogs.com/camg/archive/2022/07/19/ [] for i in list_data: print('處理=', list_data[i]) #獲取平臺類目樹資訊 platform_tree_info = Auto_update_way.platformCategoryVerify(list_data[i]) platform_tree = '->' . join([str(platform_tree_info[i][0]) for i in range(0, len(platform_tree_info))]) platform_tree_name = '->' . join([str(platform_tree_info[i][1]) for i in range(0, len(platform_tree_info))]) #獲取公司類目樹資訊 company_tree_info = Auto_update_way.companyCategoryVerify(i) company_tree = '->' . join([str(company_tree_info[i][0]) for i in range(0, len(company_tree_info))]) company_tree_name = '->' . join([str(company_tree_info[i][1]) for i in range(0, len(company_tree_info))]) value_dic = { 'platform' : 1, 'site_code' : 'MY', 'platform_category_id' : list_data[i], #平臺類目資訊 'platform_category_name' : platform_tree_info[1][1], 'platform_tree' : platform_tree, 'platform_tree_name' : platform_tree_name, 'company_category_id' : i, #公司類目資訊 'company_category_name' : company_tree_info[1][1], 'company_tree' : company_tree, 'company_tree_name' : company_tree_name, } ap_category_relation_data.append(value_dic) #得到最后映射好的資料 for i in ap_category_relation_data: print(i) print('======')
5、最后運行檔案,列印輸出

-----END
影子是一個會撒謊的精靈,它在虛空中流浪和等待被發現之間;在存在與不存在之間....
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/499743.html
標籤:其他
上一篇:【Python3】推導式
