Python TypeError:只能将str(而不是“ tuple”)连接到str

时间:2018-12-12 10:45:17

标签: python python-3.x typeerror

我正在尝试打印SQL输出,如下所示:

dwh_cur.execute("""select count (*) from sales""")
var1 = dwh_cur.fetchone()
text = 'Total Sales is ' + var1

var1 = 100

预期输出:

Total Sales is 100

但是我得到一个错误

TypeError: can only concatenate str (not "tuple") to str

2 个答案:

答案 0 :(得分:1)

dwh_cur.fetchone()返回一个元组,请尝试以下操作:

import os
import errno
import boto3
import botocore


resource = boto3.resource('s3')
bucket = resource.Bucket('my-bucket')
client = boto3.client('s3')

def download_dir():
objList = client.list_objects(Bucket='my-bucket')['Contents']
for obj in objList:
    obj_Key = obj['Key']
    path,_destPath = os.path.split(obj_Key)
    print ("Downloading file :"+ obj_Key);
    client.download_file('my-bucket', obj_Key, _destPath)

download_dir()

答案 1 :(得分:1)

好dwh_cur.fetchone()返回一条记录,该记录表示为元素的元组,

dwh_cur.execute("""select count (*) from sales""")
var1 = dwh_cur.fetchone()
text = 'Total Sales is {}'.format(var1[0])

可能会根据查询返回的内容工作。