0
我正在使用psycopg2將csv文件中的值插入到postgres數據庫中。每次我從我的csv文件中讀取新行時,我需要將開始時間增加6個小時。我試圖用以下方法實現:未將時間添加到Python中的日期對象中
date_product_str = '2015-03-05 12:00:00'
date_product = datetime.datetime.strptime(date_product_str, "%Y-%m-%d %H:00:00").date()
date_valid = date_product
interval = datetime.timedelta(hours=6)
id_location=38
in_file = open("inform_values.csv", "r")
while True:
line = in_file.readline()
if not line: break
tokens = line.split(",")
for i in range (1, 140):
cur.execute("INSERT INTO inform_tseries_data_basin_proc_fcst_prob_flow (date_product, date_valid, id_location, id_member, value) VALUES (%s, %s, %s, %s, %s)", (date_product, date_valid, id_location, i, tokens[i]))
date_valid = date_valid + interval
print date_valid
conn.commit()
cur.close()
conn.close()
但是這只是在每一步打印出2015-03-05。當我使我的間隔等於1天時:
interval = datetime.timedelta(days=1)
它按預期增加一天。爲什麼它不能工作幾個小時?