# -*- coding: utf-8 -*- import csv import uuid import os import psycopg2 from pip._vendor import chardet # G1 = 'host=210.77.68.250 port=5432 dbname=g214_test user=g214 password=g214G214' G2 = 'host=210.77.68.250 port=5432 dbname=newmediaDB3 user=newmedia password=newmedia2020!@#' if __name__ == '__main__': # with open('F:/用户激活情况表.csv','w', newline='',encoding='utf-8') as csvfile: # writer = csv.writer(csvfile) # writer.writerow( # ["姓名","电话", "单位", "职位", "是否激活", "市", "县"]) # with psycopg2.connect(G2) as connection: # with connection.cursor() as cursor: # cursor.execute( # "select * from auth_user") # for c in cursor: # with connection.cursor() as cursor1: # cursor1.execute( # "select * from dashboard_userprofile where user_id = '%s'" %(c[0])) # for c1 in cursor1: # with connection.cursor() as cursor2: # cursor2.execute( # "select * from dashboard_organization where id = '%s'" % (c1[10])) # for c2 in cursor2: # # x = None # if c1[7] == 1: # x = '已激活' # else: # x = '未激活' # writer.writerow( # [c1[1],c[4], c2[1], c1[5], x, c2[4], c2[5]]) # ------------------------------替换市和县--------------------------------------- with open('F:/用户激活情况表1.csv','w', newline='',encoding='utf-8') as csvfile: writer = csv.writer(csvfile) writer.writerow( ["姓名","电话", "单位", "职位", "是否激活", "市", "县"]) with open('F:/用户激活情况表.csv',encoding='utf-8') as csvfile: reader = csv.reader(csvfile) for r in reader: if r[0] != '姓名': name = r[0] phone= r[1] o = r[2] post = r[3] status = r[4] shi = r[5] xian = r[6] s = None s1 = None with psycopg2.connect(G2) as connection: if shi: with connection.cursor() as cursor: cursor.execute( "select * from dashboard_area_code_2020 where code = '%s'" %(shi)) for c in cursor: s = c[2] else: s = shi if xian: with connection.cursor() as cursor1: cursor1.execute( "select * from dashboard_area_code_2020 where code = '%s'" %(xian)) for c1 in cursor1: s1 = c1[2] else: s1 = xian print(s1) writer.writerow( [name,phone, o, post, status, s, s1])