首页 > 解决方案 > Python从列表中获取第二项

问题描述

我正在制作一个基本网站并使用 MySQL 来获取存储的密码数据。

从数据库获取密码的代码:

def getPassword(userID, db):
    SQL = "SELECT password FROM users WHERE user_id = %s"
    args = userID
    result, data = db.selectionQuery(SQL, args)
    print(type(data))
    if result:
        return data
    else:
        print("Error in query: ", data)
        return False

db.selectionQuery功能(很可能不相关):

def selectionQuery(self, sql, args=False):
    # Takes the SQL and any arguments. Arguments must be in the form of a tuple
    # Returns a bool value for the success of the query and the data or the error statement
    cursor = self.connectDB()
    if cursor is not False:
        if args is False:
            # No arguments
            try:
                cursor.execute(sql)
                data = cursor.fetchall()
                # Close the cursor
                cursor.close()
                self.db.close()
                return True, data
            except (pymysql.Error, pymysql.Warning) as e:
                print(e)
                return False, "Error in query." + str(e)
        else:
            # Arguments
            try:
                cursor.execute(sql, args)
                data = cursor.fetchall()
                # Close the cursor
                cursor.close()
                self.db.close()
                return True, data
            except (pymysql.Error, pymysql.Warning) as e:
                print(e)
                return False, "Error in query." + str(e)
    else:
        return False, "Error connecting to DB."

data变量是一个<class 'list'>包含[{'password': '71cf12724462a57af2effd96223f13e3cc2d11eb0259bada9abf6a5a5a50c85892cbad9b8e00a01f1b1f4c2cebfb665c34691eb320e3a61b6e22081373cbd424'}]

使用密码,我通过 validate_password 函数运行它:

def validate_password(key, password):
    split_key = wrap(key, 64)
    key_salt = bytes.fromhex(split_key[0])
    key_hash = bytes.fromhex(split_key[1])
    print("key salt is: " + str(key_salt))
    print("key hash is: " + str(key_hash))

    temp_key = hashlib.pbkdf2_hmac('sha256', password.encode('utf-8'), key_salt, 135782)

    if secrets.compare_digest(key_hash, temp_key):
        return True
    else:
        return False

得到错误:

  File "C:\x\app\lib\password.py", line 28, in validate_password
    split_key = wrap(key, 64)
  File "C:\Program Files\Python38\Lib\textwrap.py", line 379, in wrap
    return w.wrap(text)
  File "C:\Program Files\Python38\Lib\textwrap.py", line 351, in wrap
    chunks = self._split_chunks(text)
  File "C:\Program Files\Python38\Lib\textwrap.py", line 337, in _split_chunks
    text = self._munge_whitespace(text)
  File "C:\Program Files\Python38\Lib\textwrap.py", line 154, in _munge_whitespace
    text = text.expandtabs(self.tabsize)
AttributeError: 'list' object has no attribute 'expandtabs'

问题是包装只需要密码71cf12724462a57af2effd96223f13e3cc2d11eb0259bada9abf6a5a5a50c85892cbad9b8e00a01f1b1f4c2cebfb665c34691eb320e3a61b6e22081373cbd424而不是返回的内容(从数据库):[{'password': '71cf12724462a57af2effd96223f13e3cc2d11eb0259bada9abf6a5a5a50c85892cbad9b8e00a01f1b1f4c2cebfb665c34691eb320e3a61b6e22081373cbd424'}]

现在我如何从这个列表中获取实际密码[{'password': '71cf12724462a57af2effd96223f13e3cc2d11eb0259bada9abf6a5a5a50c85892cbad9b8e00a01f1b1f4c2cebfb665c34691eb320e3a61b6e22081373cbd424'}]并输入一个字符串?我尝试将其转换为数组、字符串和字典无济于事。

重申我想要的:如何将列表[{'password': '71cf12724462a57af2effd96223f13e3cc2d11eb0259bada9abf6a5a5a50c85892cbad9b8e00a01f1b1f4c2cebfb665c34691eb320e3a61b6e22081373cbd424'}] 转换为字符串:71cf12724462a57af2effd96223f13e3cc2d11eb0259bada9abf6a5a5a50c85892cbad9b8e00a01f1b1f4c2cebfb665c34691eb320e3a61b6e22081373cbd424

标签: pythonpython-3.x

解决方案


响应“如何将列表转换为字符串”:它返回列表内的字典,因此调用列表的第一个索引以仅返回字典,然后使用“密码”作为字典的键返回密码。

if result:
    return data[0]['password']

推荐阅读