drought_backup/import/importdashboardspi.py

35 lines
1.5 KiB
Python

# -*- coding: utf-8 -*-
import csv
import uuid
import os
import psycopg2
import sqlite3
from pip._vendor import chardet
G2 = 'host=210.77.68.250 port=5432 dbname=drought user=drought password=g214G214'
def insert_administrative(code1, longitude, latitude, year, month, mean_precipitation, mean_temperature):
with psycopg2.connect(G2) as connection:
with connection.cursor() as cursor:
id = uuid.uuid4()
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), code1, longitude, latitude, year, month, mean_precipitation, mean_temperature))
connection.commit()
return id
if __name__ == '__main__':
files = [os.path.join(dp, f) for dp, dn, fn in os.walk(os.path.expanduser("D:/2018/baoji10年数据")) for f in fn]
for f in files:
if f.endswith('.txt'):
with open(f) as csvfile:
for line in csvfile.readlines():
longitude, latitude, year, month, mean_precipitation, mean_temperature = line.split()
code, _ = os.path.splitext(f)
code = os.path.basename(code)
code1 = 'V7' + code
print(code1, longitude, latitude, year, month, mean_precipitation, mean_temperature)
insert_administrative(code1, longitude, latitude, year, month, mean_precipitation, mean_temperature)