python发送列表值到postgres

问题描述 投票:0回答:1

我可以用这个python脚本读取可用文件的内容。

import psycopg2
from config import config
import os

path = 'my_path'
user_id = '000'
session_id = '12345'

params = config()

conn = None
try:
    params = config()
    conn = psycopg2.connect(**params)
    cur = conn.cursor()

    for root, dirs, files in os.walk(path):
        for file in files:
            if file.endswith('.plt'):
            with open(os.path.join(root, file), 'r') as f:
                content = f.readlines()[6:]
                values = [line.strip().split(',') for line in content]
                print(values)
                query = "INSERT INTO tempo (user_id, session_id, lat, lon, flag, alt, days, gpsdate,gpstime)" \
                "VALUES (%{user_id}, %{session_id},%s,%s,%s,%s,%s,%s,%S)"
                #cur.executemany(query, values)


    cur.close()
    conn.commit()
except (Exception, psycopg2.DatabaseError) as error:
    print(error)
finally:
    if conn is not None:
        conn.close()

Output:

[['40.014395', '116.30643', '0', '492', '39751.986099537', '2008-10-30', '23:39:59'], ['40.014535', '116.306443', '0', '492', '39751.9861226852', '2008-10-30', '23:40:01'], ['40.014629', '116.30649', '0', '492', '39751.9861574074', '2008-10-30', '23:40:04'], ['40.014487', '116.306449', '0', '491', '39751.9861921296', '2008-10-30', '23:40:07'], ['40.014487', '116.306449', '0', '491', '39751.9862268518', '2008-10-30', '23:40:10'], ['40.014495', '116.306446', '0', '491', '39751.98625', '2008-10-30', '23:40:12'], ['40.014502', '116.306467', '0', '491', '39751.9863078704', '2008-10-30', '23:40:17'], ['40.014501', '116.306427', '0', '491', '39751.9863657407', '2008-10-30', '23:40:22'], ['40.014481', '116.306393', '0', '491', '39751.9864236111', '2008-10-30', '23:40:27'], ['40.013746', '116.306367', '0', '82', '39751.9864467593', '2008-10-30', '23:40:29'], ['40.01376', '116.306418', '0', '77', '39751.9865046296', '2008-10-30', '23:40:34'], ['40.013771', '116.306424', '0', '81', '39751.9865625', '2008-10-30', '23:40:39']]

[['39.984094', '116.319236', '0', '492', '39744.2451967593', '2008-10-23', '05:53:05'], ['39.984198', '116.319322', '0', '492', '39744.2452083333', '2008-10-23', '05:53:06'], ['39.984224', '116.319402', '0', '492', '39744.2452662037', '2008-10-23', '05:53:11'], ['39.984211', '116.319389', '0', '492', '39744.2453240741', '2008-10-23', '05:53:16'], ['39.984217', '116.319422', '0', '491', '39744.2453819444', '2008-10-23', '05:53:21'], ['39.98471', '116.319865', '0', '320', '39744.2454050926', '2008-10-23', '05:53:23']]

然而,当我试图将这些值发送到postgres表中时,取消了对以下内容的注释: #cur.executemany(query, values)我得到了列表超出范围的错误。

$python dump.py
list index out of range

我如何解决这个问题?

python postgresql psycopg2
1个回答
1
投票

让你的生活更轻松,只需使用 %{field_name}s 占位符。这样你就不用担心索引的问题了。顺便说一下,这个。

%{user_id}, %{session_id},%s,%s,%s,%s,%s,%s,%S)

不能用这个。

cur.executemany(query, values)

的工作,有两个原因。

1) This %{user_id} 应是 %{user_id}s

2) 命名的占位符需要从字典中获取它们的值,例如。

{"user_id": 1, "session_id": 2}

© www.soinside.com 2019 - 2024. All rights reserved.