# 更新新媒体脚本,结果为有出入的新媒体列表。 ''' 1,将excel转为csv,使用utf-8编码集。 2、获取csv内容 3、与数据库中的数据逐项对比 ''' import csv import uuid import psycopg2 as psycopg2 # code = None # alias = None # attention = None # remark = None # identificationcode = None # function = None # articleurl = None # weixinid = None # type = None G2 = 'host=210.77.68.250 port=5432 dbname=newmediaDB3 user=newmedia password=newmedia2020!@#' def set_csv_weixin(path): with open(path, encoding='utf-8') as csvfile: reader = csv.reader(csvfile) for r in reader: if r[1] == '删除': print(r) # with psycopg2.connect(G2) as connection: # with connection.cursor() as cursor: # cursor.execute( # "delete from dashboard_organization where name = '%s'" %(r[0])) # connection.commit() elif r[1] == '新增': with open('D:/2021/舆论监测平台/数据/Task_All.csv',encoding='utf-8') as c: t = csv.reader(c) for i in t: if r[0].replace(' ', '') == i[1].replace(' ', ''): print(i[1], r[0]) with psycopg2.connect(G2) as connection: with connection.cursor() as cursor: id = uuid.uuid4() cursor.execute( "insert into dashboard_organization(id,name,image, province, cities, district, town,village,directly,status,level_id,id_code, created, updated) values (%s,%s, 'danweimoren.jpg','620000000000',%s,%s,'','', %s,'1','69be9ef4-b7b7-4049-a86e-7083bee40f0e',%s,now(),now())" , (str(id), i[1],i[10],i[11],i[6],i[2])) connection.commit() if __name__ == '__main__': set_csv_weixin('D:/2021/舆论监测平台/数据/数据库更新/单位名称对比结果.csv')