psycopg2.DataError:无效的字节序列

时间:2017-11-27 00:19:33

标签: python amazon-s3 psycopg2

这是我想要做的。我想输入一个.csv文件输入到postgres数据库。我正在使用psycopg2和cur_copy_export来执行此操作。但是,我遇到了如下错误。我该怎么做才能克服这个错误?

先谢谢

错误:

    cur.copy_expert(sql=copy_sql, file=myfile)
    psycopg2.DataError: invalid byte sequence for encoding "UTF8": 0xdf 0x65
    CONTEXT:  COPY agents, line 1117

我的代码如下:

//open file from Amazon S3 Bucket
opener = urllib.URLopener()
myurl=("Amazon S3 bucket URL" + srcbucketid + "/" + file_name)
myfile=opener.open(myurl)   

copy_sql = """ COPY agents (
UniqueId,
Code,
CountryCode,
DefaultCommissionRate,
ReportingName)
FROM stdin WITH CSV HEADER DELIMITER as ',' QUOTE '\b' NULL AS ''"""

cur.copy_expert(sql=copy_sql, file=myfile)

我的数据库编码采用“UTF8”格式。我暂时无法将其更改为生产数据库。

1 个答案:

答案 0 :(得分:0)

copy_source = {'Bucket': srcbucketid, 'Key': file_name}
client.copy(copy_source, srcbucketid, 'tmp/{}'.format(file_name))
key = ('s3://'+srcbucketid+'tmp/'+file_name)
print(key)
BLOCKSIZE = 1024*1024
with s3.open('s3://'+srcbucketid+'/'+file_name, 'rb') as inf:
    with s3.open('s3://'+srcbucketid+'/tmp/'+file_name, 'wb') as ouf:
        while True:
            data = inf.read(BLOCKSIZE)
            if not data: break
            converted = data.decode('latin1').encode('utf-8')
            ouf.write(converted)