exceptions.ValueError混合迭代和读取方法会丢失数据

时间:2018-01-14 06:59:25

标签: python postgresql psycopg2

我正在尝试使用带有psycopg2和pandas的脚本从包含多个csvs的目录创建表,以便可以将表集成到postgreSQL数据库中。该脚本应该读取文件夹中的csvs,使用pd.csv_read创建pandas表以获取列名,创建表,然后将其关联的csv值复制到其中。

到目前为止,这是我的脚本:

print(a)
array(['B', 'C', 'A', 'C', 'B', 'C'],
      dtype='<U1')

这会引发以下错误:

import pandas as pd
import psycopg2
import numpy as np
import os

conn = psy.connect("host=localhost dbname=somedb user=postgres password=somepw")
cur = conn.cursor()


# make a list of all the csvs to bring into pgAdmin
path = r"C:\Data\Waste_Intervention\Census_Tables\Cleaned"
fList = os.listdir(path)

# create tables in pandas to get column names for each table
# use these values to create tables with psycopg2
for doc in fList:
    pathCSV = "{}\\{}".format(path, doc)
    tableName = doc.split("_")[-1][:-4] # this strips the descriptive part of the file path
                                        # for the table name
    table = pd.read_csv(pathCSV)

    pgCols = [] # this will hold column names and data types
    types=["integer Primary Key"]
    cols = table.columns
    colsLen = len(table.columns)
    x = 1
    while x < colsLen:
        types.append("numeric")
        x += 1
    pgTups = zip(cols, types)
    pgCols.append(', '.join([str(lst[0]) + " " + str(lst[1]) for lst in pgTups]))
    # pgcols is now in the form column1 datatype, column2 2 datatype

    statement =  'CREATE TABLE ' + tableName + '(' + pgCols[0] + ')'
    # statement used in execute

    cur.execute(statement)

    # tale is created. now use same file in csv_read to copy data into table
    with open(pathCSV, 'r') as f:
        next(f)
        cur.copy_from(f, tableName, sep=',')
    conn.commit()

我尝试使用cur.seek(0)重置光标,如下所示:Python: ValueError: Mixing iteration and read methods would lose data

但是这会引发一个错误,即游标对象没有属性&#39; seek&#39;

0 个答案:

没有答案