import copy import pandas as pd from sqlalchemy import create_engine from datetime import datetime lst=[[1,2,3,4,5,'6',7],[1,2,3,4,5,'fdf',7],[1,2,3,4,5,'fdf',6],[1,2,3,4,5,'sd',7],[1,2,3,4,5,'6',4]] # 严格去重 def remove_Duplicates_list(lst): list_store=[] # list_temp=[] # for i in list: # list_temp.append(i) list_temp =copy.deepcopy(lst) # print(list) for item in list_temp: del item[5] print(lst) ind=[] i=0 for item in list_temp: if item not in list_store: list_store.append(item) # 获得去重后的索引 ind.append(i) else: print("发现重复") i=i+1 # 去重后的结果 print(ind) list_store.clear() for item in ind: list_store.append(lst[item]) print(list_store) return list_store # remove_Duplicates_list(lst) def aa(lst): temp =copy.deepcopy(lst) for i in temp: del i[1] print(lst) # aa(lst) # # lst为要和数据库已存的数据进行比较,lst元素只需要3个字段 def is_duplicate(): # temp=copy.deepcopy(lst) # # 只保存3个字段 # after_address=[] # for item in temp: # a=[] # # 店铺名和设备编号 # a.append(item[1]) # a.append(item[2]) # # 归属时间 # a.append(item[11]) # after_address.append(a) engine = create_engine("mysql+pymysql://root:1234@localhost:3306/fume?charset=utf8") # 专门读取设备信息表 con_read = engine.connect() df = pd.read_sql('SELECT b.DI_Name,a.MV_Stat_Code,a.MV_Data_Time FROM fd_t_minutevalue AS a JOIN ea_t_device_info AS b ON a.MV_Stat_Code = b.DI_Code',con=con_read) #从设备信息表中读取设备编号,店铺名,供应商字段的数据。返回值是DateFrame类型 df['MV_Data_Time'] = df['MV_Data_Time'].dt.strftime('%Y-%m-%d %H:%M:%S') con_read.close() #关闭链接 existing_data = df.values.tolist() #DateFrame按照行转成list类型,res存放的是设备信息表中的数据 # temp =copy.deepcopy(existing_data) # for index,value in enumerate(temp): # existing_data[index][2]=value.strptime=('%Y-%m-%d %H:%M:%S') # print(value[3][11:28]) print('数据库数据为:') for i in existing_data: print(i) print(len(existing_data)) # # 保存重复值 # duplicate_data=[] # for index,value in enumerate(after_address): # if compare1(value,existing_data): # duplicate_data.append(lst[index]) # return duplicate_data # 比较 new_data仅仅为网页的表格的一行数据 也就是列表中的某一个元素 def compare1(new_data,old_data): for j in old_data: if(new_data == j): return True return False # is_duplicate() for index,value in enumerate(lst): print(index) print(value)