python - 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
?
解决方案
响应“如何将列表转换为字符串”:它返回列表内的字典,因此调用列表的第一个索引以仅返回字典,然后使用“密码”作为字典的键返回密码。
if result:
return data[0]['password']
推荐阅读
- ios - SwiftUI:实际的可触摸区域比我指定的要大
- python - 在 Windows x64 位上安装 Opencv-python 时出现问题
- python - 为什么 instagram 不能与 Selenium 无头 Chrome 一起使用?
- node.js - twilio 视频房间:主持人可以从 node.js express 端点静音房间中的参与者吗
- python - 如何在 selenium webdriver python 中找到这个按钮?
- reactjs - ReactJS 和 UIkit 3.6.5:点击事件不会在模式窗口内触发
- javascript - 无法在自定义 Hook 内的回调函数中访问状态
- python - 使用 Python 和 Beautiful Soup 进行 HTML 抓取(来自 IMDb)
- django - 将相关 ID 列表添加到 Django 查询集中的返回值
- html - css将线性渐变添加到背景不起作用,但添加单色可以