# -*- coding: utf-8 -*- import csv import uuid import os import psycopg2 from pip._vendor import chardet G2 = 'host=210.77.68.250 port=5432 dbname=brought user=brought password=g214G214' G3 = 'host=210.77.68.250 port=5432 dbname=geobrought user=brought password=g214G214' def insert_spi(code, longitude, latitude, year, month, mean_precipitation, mean_temperature): with psycopg2.connect(G2) as connection: with connection.cursor() as cursor: cursor.execute('select id from dashboard_spi where mean_precipitation = %s', (mean_precipitation,)) result = cursor.fetchone() if result: return result[0] else: id = uuid.uuid4() # print str(id), probe_code, depth, unit, drilling_id, factor1 cursor.execute( 'insert into dashboard_spi (id,code,longitude, latitude, year,month,mean_precipitation,mean_temperature, added, updated) values (%s,%s ,%s, %s, %s, %s, %s , %s, now(), now() )', (str(id), code, longitude, latitude, year, month, mean_precipitation, mean_temperature)) connection.commit() return id def select_grid_code(longitude, latitude): with psycopg2.connect(G3) as connection: with connection.cursor() as cursor: cursor.execute('select 区站号 from gridpt_ts where x=%s and y = %s', (longitude, latitude)) result = cursor.fetchone() if result: return result[0] if __name__ == '__main__': files = [os.path.join(dp, f) for dp, dn, fn in os.walk(os.path.expanduser("D:/2018/baoji/baoji")) for f in fn] for f in files: if f.endswith('.txt'): with open(f) as csvfile: reader = csv.reader(csvfile) for r in reader: for l in r: longitude = l.split()[0] latitude = l.split()[1] year = l.split()[2] month = l.split()[3] mean_precipitation = l.split()[4] mean_temperature = l.split()[5] code = select_grid_code(longitude, latitude) insert_spi(code,longitude, latitude, year, month, mean_precipitation, mean_temperature) print(mean_temperature,f)