简体   繁体   English

如何更正 stringIO() 在 aws lambda 中没有属性错误?

[英]How do I correct stringIO() has no attribute error in aws lambda?

I have a query that I'm trying to send to STDIN then to a csv file and copy command is failing over.我有一个查询,我试图将它发送到STDIN然后发送到一个 csv 文件,并且复制命令正在失败。 It seems to fall over when I call io.StringIO()当我调用io.StringIO()时它似乎倒下了

The error I'm receiving is: Error connecting to postgres instance '_io.StringIO' object has no attribute 'getValue'我收到的错误是: Error connecting to postgres instance '_io.StringIO' object has no attribute 'getValue'

My code looks like this:我的代码如下所示:

import psycopg2
from io import StringIO
import boto3
bucket = 'my_s3_bucket'
filename = 'test_data'
s3_resource = boto3.resource('s3')
conn = psycopg2.connect(host='localhost'
                        , port='5432'
                        , dbname='billing_test'
                        , user='postgres'
                        , password='password!')
cur = conn.cursor()
sql_query = "SELECT\
 * from public.customers\
  limit 1"
#cur.execute(sql_query)
#records = cur.fetchall()
#print(records)

query = '''COPY ({}) TO STDIN csv header'''.format(sql_query)

file = StringIO()
cur.copy_expert(query, file)
s3_resource.Object(bucket, f'{filename}.csv').put(Body=file.getValue())
cur.close()
conn.close()

You should read the docs, help() is your best friend in the interactive terminal:您应该阅读文档, help()是您在交互式终端中最好的朋友:

import io
help(io.StringIO)
...
 |  getvalue(self, /)
 |      Retrieve the entire contents of the object.
 |  
...

You should use getvalue() rather than getValue()您应该使用getvalue()而不是getValue()

s3_resource.Object(bucket, f'{filename}.csv').put(Body=file.getvalue())

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Music21 Midi错误:类型对象'_io.StringIO'没有属性'StringIO'。 怎么解决? - Music21 Midi Error: type object '_io.StringIO' has no attribute 'StringIO'. How to fix it? Python错误:内置函数或方法对象没有属性'StringIO' - Python error: builtin function or method object has no attribute 'StringIO' 错误“EC2”对象在 aws lambda 函数中没有属性“实例” - Error 'EC2' object has no attribute 'instances' in the aws lambda function AWS Lambda 错误:AttributeError 'list' 对象没有属性 'get' - AWS Lambda ERROR: AttributeError 'list' object has no attribute 'get' 如何清除弦乐对象? - how do I clear a stringio object? AttributeError: '_io.StringIO' object 没有属性 'StringIO' - AttributeError: '_io.StringIO' object has no attribute 'StringIO' 如何在AWS Lambda上使用Grequests? - How do I use Grequests on AWS Lambda? 如何更正Python属性错误:Float对象没有属性“ set” - How to correct Python Attribute Error: Float object has no attribute 'set' AttributeError:StringIO实例没有属性'fileno' - AttributeError: StringIO instance has no attribute 'fileno' 如何解决此错误? “‘函数’对象没有属性‘StandardScaler’” - How do I resolve this error? "'function' object has no attribute 'StandardScaler'"
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM