我得到了AttributeError:' str'对象没有属性'查询'在python中

时间:2017-09-03 09:31:53

标签: python boto3

source_table = raw_input("Enter the table name : ")
PK = raw_input("Enter the primary key : ")
PriKeyData = raw_input("Enter the data type for Primary key int-number, str-string :" )
with open('PriSortKeys.csv', 'rb') as csvfile:
    csvreader = csv.reader(csvfile, delimiter='\t', quotechar='|')
    for row in csvreader:
        if PriKeyData == "int":
                prikeyvalue = int(row[0])
            else:
                prikeyvalue = str(row[0])
            logger.info("Checking for Key :" + str(prikeyvalue))
            ## Fetching data from table based on primarykey
            sourcetable_data= source_table.query(KeyConditionExpression=Key(PK).eq(prikeyvalue))

我正在尝试将主键及其值作为用户输入并尝试查询但是我收到以下错误:

  

' STR'对象没有属性'查询'

2 个答案:

答案 0 :(得分:0)

source_table = raw_input("Enter the table name : ")

将只返回表的名称,而不是表本身。您必须首先打开表格,然后打开桌面上的query()

答案 1 :(得分:0)

source_table = raw_input("Enter the table name : ")
PK = raw_input("Enter the primary key : ")
PriKeyData = raw_input("Enter the data type for Primary key int-number, str-string :" )
dynamodb = boto3.resource('dynamodb')
s_table = dynamodb.Table(source_table.format(**locals()))
with open('PriSortKeys.csv', 'rb') as csvfile:
    csvreader = csv.reader(csvfile, delimiter='\t', quotechar='|')
    for row in csvreader:
        if PriKeyData == "int":
                prikeyvalue = int(row[0])
            else:
                prikeyvalue = str(row[0])
        logger.info("Checking for Key :" + str(prikeyvalue))
        ## Fetching data from table based on primarykey
        s_table = source_table.query(KeyConditionExpression=Key(PK).eq(prikeyvalue))