我有一系列的.csv文件有一些數據,我希望有一個Python腳本打開他們,做一些預處理,並將處理後的數據上傳到我的Postgres數據庫。許多記錄上傳到Postgres的
我有它大部分完成,但我的上傳步驟不起作用。我確定這很簡單,我錯過了,但我無法找到它。我會很感激你能提供的任何幫助。
下面的代碼:
import psycopg2
import sys
from os import listdir
from os.path import isfile, join
import csv
import re
import io
try:
con = db_connect("dbname = '[redacted]' user = '[redacted]' password = '[redacted]' host = '[redacted]'")
except:
print("Can't connect to database.")
sys.exit(1)
cur = con.cursor()
upload_file = io.StringIO()
file_list = [f for f in listdir(mypath) if isfile(join(mypath, f))]
for file in file_list:
id_match = re.search(r'.*-(\d+)\.csv', file)
if id_match:
id = id_match.group(1)
file_name = format(id_match.group())
with open(mypath+file_name) as fh:
id_reader = csv.reader(fh)
next(id_reader, None) # Skip the header row
for row in id_reader:
[stuff goes here to get desired values from file]
if upload_file.getvalue() != '': upload_file.write('\n')
upload_file.write('{0}\t{1}\t{2}'.format(id, [val1], [val2]))
print(upload_file.getvalue()) # prints output that looks like I expect it to
# with thousands of rows that seem to have the right values in the right fields
cur.copy_from(upload_file, '[my_table]', sep='\t', columns=('id', 'col_1', 'col_2'))
con.commit()
if con:
con.close()
這將運行沒有錯誤,但在PSQL選擇查詢仍然顯示在表中沒有記錄。我錯過了什麼?
編輯:我結束了放棄並將其寫入到一個臨時文件,然後上傳文件。這工作沒有任何麻煩......我顯然寧願沒有臨時文件,所以我很高興有人看到這個問題的建議。
代碼似乎沒問題。你說你的'print'行輸出了成千上萬行,但是你通過''t''作爲'copy_from'的分隔符。也許這就是問題所在? – lucasnadalutti
我正在閱讀幾十個文件,每個文件有幾百條記錄,因此總共有幾千行。代碼的這條線,使他們出現在打印語句不同的行: 如果upload_file.getvalue()=「!」:upload_file.write(「\ n」) –
是啊,這可能是問題呢。嘗試在'copy_from'參數中將'sep ='\ t''更改爲'sep ='\ n''並查看數據庫中是否有任何更改。 – lucasnadalutti