diff --git a/.gitignore b/.gitignore index b694934..95439a9 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,3 @@ -.venv \ No newline at end of file +.venv + +__pycache__ \ No newline at end of file diff --git a/dat_2_pg.py b/dat_2_pg.py index e1d534d..ab046e8 100644 --- a/dat_2_pg.py +++ b/dat_2_pg.py @@ -1,4 +1,5 @@ import psycopg +from datetime import datetime, timedelta from read_dat import read_binary_file @@ -7,22 +8,23 @@ df = read_binary_file(file_path) # PostgreSQL连接配置 postgres_config = { - "host": "192.168.3.12", - "user": "prepare", - "password": "preparepassword", - "dbname": "preparedb", + "host": "111.231.71.81", + "user": "lzflood", + "password": "lzfloodpassword", + "dbname": "lzflooddb", } postgres_conn = psycopg.connect(**postgres_config) postgres_cursor = postgres_conn.cursor() for index, row in df.iterrows(): + current_date = datetime(2010, 7, 20) + timedelta(hours=index) for i in range(len(row)): postgres_cursor.execute( - "INSERT INTO river_flow (river_id, created_at, runoff) VALUES (%s, %s, %s)", - (i + 1, index, row[i]), + "INSERT INTO river_runoff (river_id, created_at, value) VALUES (%s, %s, %s)", + (i + 1, current_date, row[i] / 86400), ) - -postgres_conn.commit() + print("index, i", index, i) + postgres_conn.commit() postgres_cursor.close() -postgres_conn.close() \ No newline at end of file +postgres_conn.close() diff --git a/read_dat.py b/read_dat.py index e984a41..39905f4 100644 --- a/read_dat.py +++ b/read_dat.py @@ -1,3 +1,4 @@ +from datetime import datetime, timedelta import struct import pandas as pd @@ -19,23 +20,31 @@ def read_binary_file(file_path): # 读取接下来的8个字节,表示日期 date_bytes = file.read(8) - date = struct.unpack(" 10: @@ -43,29 +52,30 @@ def read_binary_file(file_path): # data_bytes = file.read(8) # data = struct.unpack(" 335: - # break - # time_bytes = file.read(8) - # time = struct.unpack(" 335: + break + time_bytes = file.read(8) + time = struct.unpack("