我想使用cur.fetchmany(size)提取数据以将其写入csv文件,最后使用cur.copy_from()将数据复制到另一个表中。我知道还有其他解决方案,但是我的方法必须像data-csv-table。我有10万多行,并且正在尝试迭代cur.fetchmany()方法来部分完成该操作。在我的代码youc中,我定义了一个编写csv和迭代数据的函数。我添加了错误。我寻找了errpr,但找不到适合我问题的解决方案。我的问题是:为什么我会收到该错误?该如何解决,如下所述?
import psycopg2
import csv
connection = psycopg2.connect(user = 'user', password = 'password',host='192.850.1.45', database = 'test', port = 5432)
cur = connection.cursor()
print('Database connected successfully')
cur.execute('Select cust_ord_id::bigint, cust_ord_item_id::bigint from dwf_sales limit 10000')
def csvyaz(num):
with open('C:/Users/oguz.catal/Desktop/newfile.csv', 'wb', ) as filecsv:
writer = csv.writer(filecsv, delimiter=',', quotechar='"')
writer.writerow(num)
return 'C:/Users/oguz.catal/Desktop/newfile.csv'
def iter_row(cursor, size=1000):
while True:
rows = cursor.fetchmany(size)
num = list(sum(rows, ()))
if rows is None:
break
for row in rows:
yield row
print(num)
csvyaz(num)
t = open('C:/Users/oguz.catal/Desktop/newfile.csv', 'r+')
cur.copy_from(t,'oguz_deneme',sep=',', null='null', columns=('cust_ord_id', 'cust_ord_item_id'))
for row in iter_row(cur, 1000):
print(row)
我收到此错误:
'追踪(最近通话最近):文件 “ C:/Users/oguz.catal/PycharmProjects/untitled/fetchmannny.py”,行 34,in iter_row(cur,1000)中的行:文件 “ C:/Users/oguz.catal/PycharmProjects/untitled/fetchmannny.py”,行 31,在iter_row cur.copy_from(t,'oguz_deneme',sep =',',null ='null', 列=('cust_ord_id','cust_ord_item_id')) psycopg2.errors.BadCopyFileFormat:超出最后预期的额外数据 栏内容:COPY oguz_deneme,第1行: “ 6800008477,4400033838,6800008477,4400033834,6800008477,4400033832,6800008477,4400033827,6800008732,4 ...”'
我的csv是这样的:
6800008477 4400033838 6800008477 4400033834 6800008477 4400033832 6800008477 4400033827 6800008732 4400034887
但是据我了解,正确使用copy_from()应该是这样的;
6800008477 4400033838
6800008477 4400033834
6800008477 4400033832
6800008477 4400033827
6800008732 4400034887