Python 3.x 如何在Python结果中添加(和)

Python 3.x 如何在Python结果中添加(和),python-3.x,Python 3.x,我是新的Python3,所以请给我一个问题的答案,但我在Google上找不到答案,我让Python扫描一个文件目录,我需要在Python结果中添加一个开括号和一个新的var,以便我可以将它插入数据库。 Mysql要求将插入内容包装在val=[('test.mp4',newip)]中的括号中,因为我在运行硬编码脚本时插入了get 1 因此,我试图归档的是修改扫描结果并添加以下内容 打开/关闭括号并将新的newip插入扫描结果中,如以下示例所示 扫描结果 ['test.mp4'、'test_2.m

我是新的Python3,所以请给我一个问题的答案,但我在Google上找不到答案,我让Python扫描一个文件目录,我需要在Python结果中添加一个开括号和一个新的var,以便我可以将它插入数据库。

Mysql要求将插入内容包装在val=[('test.mp4',newip)]中的括号中,因为我在运行硬编码脚本时插入了get 1

因此,我试图归档的是修改扫描结果并添加以下内容 打开/关闭括号并将新的newip插入扫描结果中,如以下示例所示

扫描结果
['test.mp4'、'test_2.mp4'、'test_3.mp4'、test_4.mp4']

插入新结果(已修改)
[('test',newip),('test_2.mp4',newip),('test_3.mp4',newip),('test_4.mp4',newip)]



当它的作品被硬编码时

root@ubuntu:~# python3 testscan.py
['test.mp4', 'test_2.mp4', 'test_3.mp4', test_4.mp4']
1 was inserted.
请任何人建议如何实现这一点,下面是完整的代码

import os, mysql.connector,  re, uuid
files  = [f.name for f in (os.scandir('/var/www/html/media/usb1')) if f.name.endswith('.mp4')]
print(files)

newip =  (':'.join(re.findall('..', '%012x' % uuid.getnode())))

mydb = mysql.connector.connect(
  host="127.0.0.1",
  user="user",
  password="password",
  database="database"
)

mycursor = mydb.cursor()

sql = "INSERT IGNORE INTO files (file,ip) VALUES (%s,%s)"
val =[('test.mp4',newip)]

mycursor.executemany(sql, val)

mydb.commit()

print(mycursor.rowcount, "was inserted.")

因此,如果要将新IP添加到扫描中,可以使用列表:

    files = ['test.mp4', 'test_2.mp4', 'test_3.mp4', 'test_4.mp4']

    sql_values = [(file, newip) for file in files]
结果如下所示:

    [('test.mp4', newip), ('test2.mp4', newip), ('test3.mp4', newip), ('test4.mp4', newip)]

实际上,您的代码在更新了主机后缺少了一个结尾。
host=“127.0.0.1,
更新了主机。谢谢,您让它看起来很简单,整晚都在努力解决这个问题。插入了4个,没问题,伙计。我们从每一个挑战中学习。我们都是在一段时间前开始编写代码的^^