两个问题:所以我有一个文件名列表,我想将每个文件名输入到 MYSQL 查询中。第一个问题是如何遍历文件列表并将元素(文件名)作为变量传递给 MYSQL?第二个问题是:如何以更优雅的方式打印结果而不使用括号和 L 的形式返回的元组输出?我下面的方式适用于三列,但我想要一种灵活的方式,当我获取更多行时,我不必添加子列表(cleaned1、2..)。任何帮助都非常感谢!!!
MyConnection = MySQLdb.connect( host = "localhost", user = "root", \
passwd = "xxxx", db = "xxxx")
MyCursor = MyConnection.cursor()
**MyList= (File1, File2, File3, File...., File36)
For i in Mylist:
do MYSQL query**
SQL = """SELECT a.column1, a.column2, b.column2 FROM **i in MyList** a, table2 b WHERE
a.column1=b.column1;"""
SQLLen = MyCursor.execute(SQL) # returns the number of records retrieved
AllOut = MyCursor.fetchall()
**List = list(AllOut) # this puts all the TUple information into a list
cleaned = [i[0] for i in List] # this cleans up the Tuple characters)
cleaned1 = [i[1] for i in List] # this cleans up the Tuple characters)
cleaned2 = [i[2] for i in List] # this cleans up the Tuple characters)
NewList=zip(cleaned,cleaned1,cleaned2) # This makes a new List
print NewList[0:10]**
# Close the files
MyCursor.close()
MyConnection.close()
我可以弄清楚保存到文件,但我不知道如何将 python 变量传递给 MYSQL。