python - 如何在python中拆分元组并获取具有列名的数据
问题描述
import cv2
import NumPy as np
import face_recognition
import os
from DateTime import DateTime
from SQL import mydb
mycursor = mydb.cursor()
mycursor.execute("SELECT name, age,imageURL,gender FROM staff")
myresult = mycursor.fetchone()
name = myresult[0]
age = myresult[1]
imgs = myresult[2]
gender = myresult[3]
需要拆分来自 mySQl 的元组
当前结果:
[(2, 'Joe', '30', 'Male', 'images/Joe.jpg'), (3, 'Doe', '28', 'Male', 'images/Doe.jpg')]
必需的:
(2, 'Joe', '30', 'Male', 'images/Joe.jpg')
(3, 'Doe', '28', 'Male', 'images/Doe.jpg')
之后需要将其放入列中
ID Name Age Gender imageURL
2 Joe 30 Male images/Joe.jpg
3 Doe 28 Male images/Doe.jpg
应用贾斯汀的建议后:
import mysql.connector
mydb = mysql.connector.connect(
host="localhost",
user="root",
password="",
database = "face_db"
)
mycursor = mydb.cursor()
mycursor.execute("SELECT * FROM staff")
myresult = mycursor.fetchall()
for (ID, Name, Age, Gender, imageURL) in myresult:
print(ID,Name,Age,Gender,imageURL)
从这里我有单独的 ID、姓名、年龄和性别,但缺少列名
解决方案
这称为元组解包。
print("ID", "Name", "Age", "Gender", "imageURL") # print your headers
for (ID, Name, Age, Gender, imageURL) in myresult:
print(ID, Name, Age, Gender, imageURL) # print the values
推荐阅读
- html - Tradingview Widget - 尝试自定义移动平均线的长度
- android - 多平台移动应用程序开发
- react-native - 搬迁世博入口点后的空白屏幕
- java - 在 Java 8 中使用 stream() 到达 json 中的元素
- java - 如何创建包含所有 Maven 依赖项的不可执行 JAR 文件
- javascript - 在返回 JSON 对象之前在 MongoDB(猫鼬)中进行多个查询
- apache - Apache 中的 403 访问拒绝无法访问 ErrorDocument 403
- python - Wand - 如何使用 Python 更改和替换现有图像文件
- javascript - 开玩笑测试模拟函数返回未定义(创建反应应用项目)
- python - 尝试以 .format 进行数学运算