desertvegetationcommunitysu.../import_area.py

44 lines
1.5 KiB
Python
Raw Normal View History

2020-07-12 12:40:52 +00:00
# -*- coding: utf-8 -*-
import csv
import re
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=communityDB user=community password=community!@#'
def insert_area(code, name,level, pcode):
with psycopg2.connect(G2) as connection:
with connection.cursor() as cursor:
cursor.execute(
'insert into dashboard_area_code_2020(code, name,level, pcode) values (%s,%s, %s,%s)'
, (code, name,level, pcode))
connection.commit()
return code
if __name__ == '__main__':
f = open('D:/2020/荒漠植被调查/china_area-master/area_code_2020.sql/area_code_2020.sql',encoding='utf-8').read()
data = f.split('INSERT INTO dashboard_area_code_2020 (code, name, level, pcode)VALUES')
for d in data:
for i in d.split('\n'):
list = i.split(');')[0].split('),')[0].split('(')
# print(len(list))
if len(list) > 1:
success_data = list[1].split(',')
try:
code = success_data[0]
name = re.sub("'","",success_data[1])
level = success_data[2]
pcode = success_data[3]
print(code, name, level, pcode)
insert_area(code, name,level, pcode)
except:
pass