2017-09-03 29 views
-1
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)) 

我想把主键和它的值作为用户输入并试图查询,但我得到以下错误:我越来越AttributeError:'str'对象在python中没有属性'查询'

'str' object has no attribute 'query'

+1

'source_table = raw_input()'...那总是一个'str' ...你试图运行query()对什么对象? –

回答

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

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

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))